<!DOCTYPE html>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<meta name="generator" content="ApiGen 2.6.1" />
	<meta name="robots" content="noindex" />

	<title>File libraries/REST_Controller.php</title>

	<script type="text/javascript" src="resources/combined.js?144019575"></script>
	<script type="text/javascript" src="elementlist.js?2260620923"></script>
	<link rel="stylesheet" type="text/css" media="all" href="resources/style.css?1532301413" />

</head>

<body>
<div id="left">
	<div id="menu">
		<a href="index.html" title="Overview"><span>Overview</span></a>


		<div id="groups">
			<h3>Packages</h3>
			<ul>
				<li class="active"><a href="package-CodeIgniter.html">CodeIgniter<span></span></a>
						<ul>
				<li class="active"><a href="package-CodeIgniter.Libraries.html">Libraries</a>
						</li>
							</ul></li>
				<li><a href="package-None.html">None</a>
						</li>
			</ul>
		</div>

		<hr />


		<div id="elements">
			<h3>Classes</h3>
			<ul>
				<li><a href="class-Acciones.html">Acciones</a></li>
				<li><a href="class-Datatables.html">Datatables</a></li>
				<li class="active"><a href="class-REST_Controller.html">REST_Controller</a></li>
				<li><a href="class-Usuarios.html">Usuarios</a></li>
			</ul>





		</div>
	</div>
</div>

<div id="splitter"></div>

<div id="right">
<div id="rightInner">
	<form id="search">
		<input type="hidden" name="cx" value="" />
		<input type="hidden" name="ie" value="UTF-8" />
		<input type="text" name="q" class="text" />
		<input type="submit" value="Search" />
	</form>

	<div id="navigation">
		<ul>
			<li>
				<a href="index.html" title="Overview"><span>Overview</span></a>
			</li>
			<li>
				<a href="package-CodeIgniter.Libraries.html" title="Summary of CodeIgniter\Libraries"><span>Package</span></a>
			</li>
			<li>
				<a href="class-REST_Controller.html" title="Summary of REST_Controller"><span>Class</span></a>
			</li>
		</ul>
		<ul>
			<li>
				<a href="tree.html" title="Tree view of classes, interfaces, traits and exceptions"><span>Tree</span></a>
			</li>
		</ul>
		<ul>
		</ul>
	</div>

<pre><code><a href="#1" id="1" class="l">   1: </a><span class="xlang">&lt;?php</span> <span class="php-keyword2">defined</span>(<span class="php-quote">'BASEPATH'</span>) <span class="php-keyword1">OR</span> <span class="php-keyword1">exit</span>(<span class="php-quote">'No direct script access allowed'</span>);
<a href="#2" id="2" class="l">   2: </a>
<a href="#3" id="3" class="l">   3: </a><span class="php-comment">/**
</span><a href="#4" id="4" class="l">   4: </a><span class="php-comment"> * CodeIgniter Rest Controller
</span><a href="#5" id="5" class="l">   5: </a><span class="php-comment"> *
</span><a href="#6" id="6" class="l">   6: </a><span class="php-comment"> * A fully RESTful server implementation for CodeIgniter using one library, one config file and one controller.
</span><a href="#7" id="7" class="l">   7: </a><span class="php-comment"> *
</span><a href="#8" id="8" class="l">   8: </a><span class="php-comment"> * @package         CodeIgniter
</span><a href="#9" id="9" class="l">   9: </a><span class="php-comment"> * @subpackage      Libraries
</span><a href="#10" id="10" class="l">  10: </a><span class="php-comment"> * @category        Libraries
</span><a href="#11" id="11" class="l">  11: </a><span class="php-comment"> * @author          Phil Sturgeon
</span><a href="#12" id="12" class="l">  12: </a><span class="php-comment"> * @license         http://philsturgeon.co.uk/code/dbad-license
</span><a href="#13" id="13" class="l">  13: </a><span class="php-comment"> * @link            https://github.com/philsturgeon/codeigniter-restserver
</span><a href="#14" id="14" class="l">  14: </a><span class="php-comment"> * @version         2.6.0
</span><a href="#15" id="15" class="l">  15: </a><span class="php-comment"> */</span>
<a href="#16" id="16" class="l">  16: </a><span class="php-keyword1">abstract</span> <span class="php-keyword1">class</span> REST_Controller <span class="php-keyword1">extends</span> CI_Controller
<a href="#17" id="17" class="l">  17: </a>{
<a href="#18" id="18" class="l">  18: </a>    <span class="php-comment">/**
</span><a href="#19" id="19" class="l">  19: </a><span class="php-comment">     * This defines the rest format.
</span><a href="#20" id="20" class="l">  20: </a><span class="php-comment">     * 
</span><a href="#21" id="21" class="l">  21: </a><span class="php-comment">     * Must be overriden it in a controller so that it is set.
</span><a href="#22" id="22" class="l">  22: </a><span class="php-comment">     *
</span><a href="#23" id="23" class="l">  23: </a><span class="php-comment">     * @var string|null
</span><a href="#24" id="24" class="l">  24: </a><span class="php-comment">     */</span>
<a href="#25" id="25" class="l">  25: </a>    <span class="php-keyword1">protected</span> <span class="php-var">$rest_format</span> = <span class="php-keyword1">NULL</span>;
<a href="#26" id="26" class="l">  26: </a>
<a href="#27" id="27" class="l">  27: </a>    <span class="php-comment">/**
</span><a href="#28" id="28" class="l">  28: </a><span class="php-comment">     * Defines the list of method properties such as limit, log and level
</span><a href="#29" id="29" class="l">  29: </a><span class="php-comment">     *
</span><a href="#30" id="30" class="l">  30: </a><span class="php-comment">     * @var array 
</span><a href="#31" id="31" class="l">  31: </a><span class="php-comment">     */</span>
<a href="#32" id="32" class="l">  32: </a>    <span class="php-keyword1">protected</span> <span class="php-var">$methods</span> = <span class="php-keyword1">array</span>();
<a href="#33" id="33" class="l">  33: </a>
<a href="#34" id="34" class="l">  34: </a>    <span class="php-comment">/**
</span><a href="#35" id="35" class="l">  35: </a><span class="php-comment">     * General request data and information.
</span><a href="#36" id="36" class="l">  36: </a><span class="php-comment">     * Stores accept, language, body, headers, etc.
</span><a href="#37" id="37" class="l">  37: </a><span class="php-comment">     *
</span><a href="#38" id="38" class="l">  38: </a><span class="php-comment">     * @var object 
</span><a href="#39" id="39" class="l">  39: </a><span class="php-comment">     */</span>
<a href="#40" id="40" class="l">  40: </a>    <span class="php-keyword1">protected</span> <span class="php-var">$request</span> = <span class="php-keyword1">NULL</span>;
<a href="#41" id="41" class="l">  41: </a>
<a href="#42" id="42" class="l">  42: </a>    <span class="php-comment">/**
</span><a href="#43" id="43" class="l">  43: </a><span class="php-comment">     * What is gonna happen in output?
</span><a href="#44" id="44" class="l">  44: </a><span class="php-comment">     * 
</span><a href="#45" id="45" class="l">  45: </a><span class="php-comment">     * @var object 
</span><a href="#46" id="46" class="l">  46: </a><span class="php-comment">     */</span>
<a href="#47" id="47" class="l">  47: </a>    <span class="php-keyword1">protected</span> <span class="php-var">$response</span> = <span class="php-keyword1">NULL</span>;
<a href="#48" id="48" class="l">  48: </a>
<a href="#49" id="49" class="l">  49: </a>    <span class="php-comment">/**
</span><a href="#50" id="50" class="l">  50: </a><span class="php-comment">     * Stores DB, keys, key level, etc
</span><a href="#51" id="51" class="l">  51: </a><span class="php-comment">     *
</span><a href="#52" id="52" class="l">  52: </a><span class="php-comment">     * @var object 
</span><a href="#53" id="53" class="l">  53: </a><span class="php-comment">     */</span>
<a href="#54" id="54" class="l">  54: </a>    <span class="php-keyword1">protected</span> <span class="php-var">$rest</span> = <span class="php-keyword1">NULL</span>;
<a href="#55" id="55" class="l">  55: </a>
<a href="#56" id="56" class="l">  56: </a>    <span class="php-comment">/**
</span><a href="#57" id="57" class="l">  57: </a><span class="php-comment">     * The arguments for the GET request method
</span><a href="#58" id="58" class="l">  58: </a><span class="php-comment">     *
</span><a href="#59" id="59" class="l">  59: </a><span class="php-comment">     * @var array 
</span><a href="#60" id="60" class="l">  60: </a><span class="php-comment">     */</span>
<a href="#61" id="61" class="l">  61: </a>    <span class="php-keyword1">protected</span> <span class="php-var">$_get_args</span> = <span class="php-keyword1">array</span>();
<a href="#62" id="62" class="l">  62: </a>
<a href="#63" id="63" class="l">  63: </a>    <span class="php-comment">/**
</span><a href="#64" id="64" class="l">  64: </a><span class="php-comment">     * The arguments for the POST request method
</span><a href="#65" id="65" class="l">  65: </a><span class="php-comment">     * 
</span><a href="#66" id="66" class="l">  66: </a><span class="php-comment">     * @var array
</span><a href="#67" id="67" class="l">  67: </a><span class="php-comment">     */</span>
<a href="#68" id="68" class="l">  68: </a>    <span class="php-keyword1">protected</span> <span class="php-var">$_post_args</span> = <span class="php-keyword1">array</span>();
<a href="#69" id="69" class="l">  69: </a>
<a href="#70" id="70" class="l">  70: </a>    <span class="php-comment">/**
</span><a href="#71" id="71" class="l">  71: </a><span class="php-comment">     * The arguments for the PUT request method
</span><a href="#72" id="72" class="l">  72: </a><span class="php-comment">     *
</span><a href="#73" id="73" class="l">  73: </a><span class="php-comment">     * @var array
</span><a href="#74" id="74" class="l">  74: </a><span class="php-comment">     */</span>
<a href="#75" id="75" class="l">  75: </a>    <span class="php-keyword1">protected</span> <span class="php-var">$_put_args</span> = <span class="php-keyword1">array</span>();
<a href="#76" id="76" class="l">  76: </a>
<a href="#77" id="77" class="l">  77: </a>    <span class="php-comment">/**
</span><a href="#78" id="78" class="l">  78: </a><span class="php-comment">     * The arguments for the DELETE request method
</span><a href="#79" id="79" class="l">  79: </a><span class="php-comment">     *
</span><a href="#80" id="80" class="l">  80: </a><span class="php-comment">     * @var array
</span><a href="#81" id="81" class="l">  81: </a><span class="php-comment">     */</span>
<a href="#82" id="82" class="l">  82: </a>    <span class="php-keyword1">protected</span> <span class="php-var">$_delete_args</span> = <span class="php-keyword1">array</span>();
<a href="#83" id="83" class="l">  83: </a>
<a href="#84" id="84" class="l">  84: </a>    <span class="php-comment">/**
</span><a href="#85" id="85" class="l">  85: </a><span class="php-comment">     * The arguments from GET, POST, PUT, DELETE request methods combined.
</span><a href="#86" id="86" class="l">  86: </a><span class="php-comment">     *
</span><a href="#87" id="87" class="l">  87: </a><span class="php-comment">     * @var array
</span><a href="#88" id="88" class="l">  88: </a><span class="php-comment">     */</span>
<a href="#89" id="89" class="l">  89: </a>    <span class="php-keyword1">protected</span> <span class="php-var">$_args</span> = <span class="php-keyword1">array</span>();
<a href="#90" id="90" class="l">  90: </a>
<a href="#91" id="91" class="l">  91: </a>    <span class="php-comment">/**
</span><a href="#92" id="92" class="l">  92: </a><span class="php-comment">     * If the request is allowed based on the API key provided.
</span><a href="#93" id="93" class="l">  93: </a><span class="php-comment">     * 
</span><a href="#94" id="94" class="l">  94: </a><span class="php-comment">     * @var boolean
</span><a href="#95" id="95" class="l">  95: </a><span class="php-comment">     */</span>
<a href="#96" id="96" class="l">  96: </a>    <span class="php-keyword1">protected</span> <span class="php-var">$_allow</span> = <span class="php-keyword1">TRUE</span>;
<a href="#97" id="97" class="l">  97: </a>
<a href="#98" id="98" class="l">  98: </a>    <span class="php-comment">/**
</span><a href="#99" id="99" class="l">  99: </a><span class="php-comment">     * Determines if output compression is enabled
</span><a href="#100" id="100" class="l"> 100: </a><span class="php-comment">     * 
</span><a href="#101" id="101" class="l"> 101: </a><span class="php-comment">     * @var boolean
</span><a href="#102" id="102" class="l"> 102: </a><span class="php-comment">     */</span>
<a href="#103" id="103" class="l"> 103: </a>    <span class="php-keyword1">protected</span> <span class="php-var">$_zlib_oc</span> = <span class="php-keyword1">FALSE</span>;
<a href="#104" id="104" class="l"> 104: </a>
<a href="#105" id="105" class="l"> 105: </a>    <span class="php-comment">/**
</span><a href="#106" id="106" class="l"> 106: </a><span class="php-comment">     * List all supported methods, the first will be the default format
</span><a href="#107" id="107" class="l"> 107: </a><span class="php-comment">     *
</span><a href="#108" id="108" class="l"> 108: </a><span class="php-comment">     * @var array
</span><a href="#109" id="109" class="l"> 109: </a><span class="php-comment">     */</span>
<a href="#110" id="110" class="l"> 110: </a>    <span class="php-keyword1">protected</span> <span class="php-var">$_supported_formats</span> = <span class="php-keyword1">array</span>(
<a href="#111" id="111" class="l"> 111: </a>        <span class="php-quote">'xml'</span> =&gt; <span class="php-quote">'application/xml'</span>,
<a href="#112" id="112" class="l"> 112: </a>        <span class="php-quote">'json'</span> =&gt; <span class="php-quote">'application/json'</span>,
<a href="#113" id="113" class="l"> 113: </a>        <span class="php-quote">'jsonp'</span> =&gt; <span class="php-quote">'application/javascript'</span>,
<a href="#114" id="114" class="l"> 114: </a>        <span class="php-quote">'serialized'</span> =&gt; <span class="php-quote">'application/vnd.php.serialized'</span>,
<a href="#115" id="115" class="l"> 115: </a>        <span class="php-quote">'php'</span> =&gt; <span class="php-quote">'text/plain'</span>,
<a href="#116" id="116" class="l"> 116: </a>        <span class="php-quote">'html'</span> =&gt; <span class="php-quote">'text/html'</span>,
<a href="#117" id="117" class="l"> 117: </a>        <span class="php-quote">'csv'</span> =&gt; <span class="php-quote">'application/csv'</span>
<a href="#118" id="118" class="l"> 118: </a>    );
<a href="#119" id="119" class="l"> 119: </a>
<a href="#120" id="120" class="l"> 120: </a>    <span class="php-comment">/**
</span><a href="#121" id="121" class="l"> 121: </a><span class="php-comment">     * Developers can extend this class and add a check in here.
</span><a href="#122" id="122" class="l"> 122: </a><span class="php-comment">     */</span>
<a href="#123" id="123" class="l"> 123: </a>    <span class="php-keyword1">protected</span> <span class="php-keyword1">function</span> early_checks()
<a href="#124" id="124" class="l"> 124: </a>    {
<a href="#125" id="125" class="l"> 125: </a>        
<a href="#126" id="126" class="l"> 126: </a>    }
<a href="#127" id="127" class="l"> 127: </a>
<a href="#128" id="128" class="l"> 128: </a>    <span class="php-comment">/**
</span><a href="#129" id="129" class="l"> 129: </a><span class="php-comment">     * Constructor function
</span><a href="#130" id="130" class="l"> 130: </a><span class="php-comment">     * @todo Document more please.
</span><a href="#131" id="131" class="l"> 131: </a><span class="php-comment">     */</span>
<a href="#132" id="132" class="l"> 132: </a>    <span class="php-keyword1">public</span> <span class="php-keyword1">function</span> __construct()
<a href="#133" id="133" class="l"> 133: </a>    {
<a href="#134" id="134" class="l"> 134: </a>        parent::__construct();
<a href="#135" id="135" class="l"> 135: </a>
<a href="#136" id="136" class="l"> 136: </a>        <span class="php-var">$this</span>-&gt;_zlib_oc = @<span class="php-keyword2">ini_get</span>(<span class="php-quote">'zlib.output_compression'</span>);
<a href="#137" id="137" class="l"> 137: </a>
<a href="#138" id="138" class="l"> 138: </a>        <span class="php-comment">// Lets grab the config and get ready to party</span>
<a href="#139" id="139" class="l"> 139: </a>        <span class="php-var">$this</span>-&gt;load-&gt;config(<span class="php-quote">'rest'</span>);
<a href="#140" id="140" class="l"> 140: </a>
<a href="#141" id="141" class="l"> 141: </a>        <span class="php-comment">// How is this request being made? POST, DELETE, GET, PUT?</span>
<a href="#142" id="142" class="l"> 142: </a>        <span class="php-var">$this</span>-&gt;request = <span class="php-keyword1">new</span> stdClass();
<a href="#143" id="143" class="l"> 143: </a>        <span class="php-var">$this</span>-&gt;request-&gt;method = <span class="php-var">$this</span>-&gt;_detect_method();
<a href="#144" id="144" class="l"> 144: </a>
<a href="#145" id="145" class="l"> 145: </a>        <span class="php-comment">// Set up our GET variables</span>
<a href="#146" id="146" class="l"> 146: </a>        <span class="php-var">$this</span>-&gt;_get_args = <span class="php-keyword2">array_merge</span>(<span class="php-var">$this</span>-&gt;_get_args, <span class="php-var">$this</span>-&gt;uri-&gt;ruri_to_assoc());
<a href="#147" id="147" class="l"> 147: </a>
<a href="#148" id="148" class="l"> 148: </a>        <span class="php-comment">//$this-&gt;load-&gt;library('security');</span>
<a href="#149" id="149" class="l"> 149: </a>
<a href="#150" id="150" class="l"> 150: </a>        <span class="php-comment">// This library is bundled with REST_Controller 2.5+, but will eventually be part of CodeIgniter itself</span>
<a href="#151" id="151" class="l"> 151: </a>        <span class="php-var">$this</span>-&gt;load-&gt;library(<span class="php-quote">'format'</span>);
<a href="#152" id="152" class="l"> 152: </a>
<a href="#153" id="153" class="l"> 153: </a>        <span class="php-comment">// Try to find a format for the request (means we have a request body)</span>
<a href="#154" id="154" class="l"> 154: </a>        <span class="php-var">$this</span>-&gt;request-&gt;format = <span class="php-var">$this</span>-&gt;_detect_input_format();
<a href="#155" id="155" class="l"> 155: </a>
<a href="#156" id="156" class="l"> 156: </a>        <span class="php-comment">// Some Methods cant have a body</span>
<a href="#157" id="157" class="l"> 157: </a>        <span class="php-var">$this</span>-&gt;request-&gt;body = <span class="php-keyword1">NULL</span>;
<a href="#158" id="158" class="l"> 158: </a>
<a href="#159" id="159" class="l"> 159: </a>        <span class="php-keyword1">switch</span> (<span class="php-var">$this</span>-&gt;request-&gt;method)
<a href="#160" id="160" class="l"> 160: </a>        {
<a href="#161" id="161" class="l"> 161: </a>            <span class="php-keyword1">case</span> <span class="php-quote">'get'</span>:
<a href="#162" id="162" class="l"> 162: </a>                <span class="php-comment">// Grab proper GET variables</span>
<a href="#163" id="163" class="l"> 163: </a>                <span class="php-keyword2">parse_str</span>(<span class="php-keyword2">parse_url</span>(<span class="php-var">$_SERVER</span>[<span class="php-quote">'REQUEST_URI'</span>], PHP_URL_QUERY), <span class="php-var">$get</span>);
<a href="#164" id="164" class="l"> 164: </a>
<a href="#165" id="165" class="l"> 165: </a>                <span class="php-comment">// Merge both the URI segements and GET params</span>
<a href="#166" id="166" class="l"> 166: </a>                <span class="php-var">$this</span>-&gt;_get_args = <span class="php-keyword2">array_merge</span>(<span class="php-var">$this</span>-&gt;_get_args, <span class="php-var">$get</span>);
<a href="#167" id="167" class="l"> 167: </a>                <span class="php-keyword1">break</span>;
<a href="#168" id="168" class="l"> 168: </a>
<a href="#169" id="169" class="l"> 169: </a>            <span class="php-keyword1">case</span> <span class="php-quote">'post'</span>:
<a href="#170" id="170" class="l"> 170: </a>                <span class="php-var">$this</span>-&gt;_post_args = <span class="php-var">$_POST</span>;
<a href="#171" id="171" class="l"> 171: </a>
<a href="#172" id="172" class="l"> 172: </a>                <span class="php-var">$this</span>-&gt;request-&gt;format <span class="php-keyword1">and</span> <span class="php-var">$this</span>-&gt;request-&gt;body = <span class="php-keyword2">file_get_contents</span>(<span class="php-quote">'php://input'</span>);
<a href="#173" id="173" class="l"> 173: </a>                <span class="php-keyword1">break</span>;
<a href="#174" id="174" class="l"> 174: </a>
<a href="#175" id="175" class="l"> 175: </a>            <span class="php-keyword1">case</span> <span class="php-quote">'put'</span>:
<a href="#176" id="176" class="l"> 176: </a>                <span class="php-comment">// It might be a HTTP body</span>
<a href="#177" id="177" class="l"> 177: </a>                <span class="php-keyword1">if</span> (<span class="php-var">$this</span>-&gt;request-&gt;format)
<a href="#178" id="178" class="l"> 178: </a>                {
<a href="#179" id="179" class="l"> 179: </a>                    <span class="php-var">$this</span>-&gt;request-&gt;body = <span class="php-keyword2">file_get_contents</span>(<span class="php-quote">'php://input'</span>);
<a href="#180" id="180" class="l"> 180: </a>                }
<a href="#181" id="181" class="l"> 181: </a>
<a href="#182" id="182" class="l"> 182: </a>                <span class="php-comment">// If no file type is provided, this is probably just arguments</span>
<a href="#183" id="183" class="l"> 183: </a>                <span class="php-keyword1">else</span>
<a href="#184" id="184" class="l"> 184: </a>                {
<a href="#185" id="185" class="l"> 185: </a>                    <span class="php-keyword2">parse_str</span>(<span class="php-keyword2">file_get_contents</span>(<span class="php-quote">'php://input'</span>), <span class="php-var">$this</span>-&gt;_put_args);
<a href="#186" id="186" class="l"> 186: </a>                }
<a href="#187" id="187" class="l"> 187: </a>
<a href="#188" id="188" class="l"> 188: </a>                <span class="php-keyword1">break</span>;
<a href="#189" id="189" class="l"> 189: </a>
<a href="#190" id="190" class="l"> 190: </a>            <span class="php-keyword1">case</span> <span class="php-quote">'delete'</span>:
<a href="#191" id="191" class="l"> 191: </a>                <span class="php-comment">// Set up out DELETE variables (which shouldn't really exist, but sssh!)</span>
<a href="#192" id="192" class="l"> 192: </a>                <span class="php-keyword2">parse_str</span>(<span class="php-keyword2">file_get_contents</span>(<span class="php-quote">'php://input'</span>), <span class="php-var">$this</span>-&gt;_delete_args);
<a href="#193" id="193" class="l"> 193: </a>                <span class="php-keyword1">break</span>;
<a href="#194" id="194" class="l"> 194: </a>        }
<a href="#195" id="195" class="l"> 195: </a>
<a href="#196" id="196" class="l"> 196: </a>        <span class="php-comment">// Now we know all about our request, let's try and parse the body if it exists</span>
<a href="#197" id="197" class="l"> 197: </a>        <span class="php-keyword1">if</span> (<span class="php-var">$this</span>-&gt;request-&gt;format <span class="php-keyword1">and</span> <span class="php-var">$this</span>-&gt;request-&gt;body)
<a href="#198" id="198" class="l"> 198: </a>        {
<a href="#199" id="199" class="l"> 199: </a>            <span class="php-var">$this</span>-&gt;request-&gt;body = <span class="php-var">$this</span>-&gt;format-&gt;factory(<span class="php-var">$this</span>-&gt;request-&gt;body, <span class="php-var">$this</span>-&gt;request-&gt;format)-&gt;to_array();
<a href="#200" id="200" class="l"> 200: </a>            <span class="php-comment">// Assign payload arguments to proper method container</span>
<a href="#201" id="201" class="l"> 201: </a>            <span class="php-var">$this</span>-&gt;{<span class="php-quote">'_'</span>.<span class="php-var">$this</span>-&gt;request-&gt;method.<span class="php-quote">'_args'</span>} = <span class="php-var">$this</span>-&gt;request-&gt;body;
<a href="#202" id="202" class="l"> 202: </a>        }
<a href="#203" id="203" class="l"> 203: </a>
<a href="#204" id="204" class="l"> 204: </a>        <span class="php-comment">// Merge both for one mega-args variable</span>
<a href="#205" id="205" class="l"> 205: </a>        <span class="php-var">$this</span>-&gt;_args = <span class="php-keyword2">array_merge</span>(<span class="php-var">$this</span>-&gt;_get_args, <span class="php-var">$this</span>-&gt;_put_args, <span class="php-var">$this</span>-&gt;_post_args, <span class="php-var">$this</span>-&gt;_delete_args);
<a href="#206" id="206" class="l"> 206: </a>
<a href="#207" id="207" class="l"> 207: </a>        <span class="php-comment">// Which format should the data be returned in?</span>
<a href="#208" id="208" class="l"> 208: </a>        <span class="php-var">$this</span>-&gt;response = <span class="php-keyword1">new</span> stdClass();
<a href="#209" id="209" class="l"> 209: </a>        <span class="php-var">$this</span>-&gt;response-&gt;format = <span class="php-var">$this</span>-&gt;_detect_output_format();
<a href="#210" id="210" class="l"> 210: </a>
<a href="#211" id="211" class="l"> 211: </a>        <span class="php-comment">// Which format should the data be returned in?</span>
<a href="#212" id="212" class="l"> 212: </a>        <span class="php-var">$this</span>-&gt;response-&gt;lang = <span class="php-var">$this</span>-&gt;_detect_lang();
<a href="#213" id="213" class="l"> 213: </a>
<a href="#214" id="214" class="l"> 214: </a>        <span class="php-comment">// Developers can extend this class and add a check in here</span>
<a href="#215" id="215" class="l"> 215: </a>        <span class="php-var">$this</span>-&gt;early_checks();
<a href="#216" id="216" class="l"> 216: </a>
<a href="#217" id="217" class="l"> 217: </a>        <span class="php-comment">// Check if there is a specific auth type for the current class/method</span>
<a href="#218" id="218" class="l"> 218: </a>        <span class="php-var">$this</span>-&gt;auth_override = <span class="php-var">$this</span>-&gt;_auth_override_check();
<a href="#219" id="219" class="l"> 219: </a>
<a href="#220" id="220" class="l"> 220: </a>        <span class="php-comment">// When there is no specific override for the current class/method, use the default auth value set in the config</span>
<a href="#221" id="221" class="l"> 221: </a>        <span class="php-keyword1">if</span> (<span class="php-var">$this</span>-&gt;auth_override !== <span class="php-keyword1">TRUE</span>)
<a href="#222" id="222" class="l"> 222: </a>        {
<a href="#223" id="223" class="l"> 223: </a>            <span class="php-keyword1">if</span> (<span class="php-var">$this</span>-&gt;config-&gt;item(<span class="php-quote">'rest_auth'</span>) == <span class="php-quote">'basic'</span>)
<a href="#224" id="224" class="l"> 224: </a>            {
<a href="#225" id="225" class="l"> 225: </a>                <span class="php-var">$this</span>-&gt;_prepare_basic_auth();
<a href="#226" id="226" class="l"> 226: </a>            }
<a href="#227" id="227" class="l"> 227: </a>            <span class="php-keyword1">elseif</span> (<span class="php-var">$this</span>-&gt;config-&gt;item(<span class="php-quote">'rest_auth'</span>) == <span class="php-quote">'digest'</span>)
<a href="#228" id="228" class="l"> 228: </a>            {
<a href="#229" id="229" class="l"> 229: </a>                <span class="php-var">$this</span>-&gt;_prepare_digest_auth();
<a href="#230" id="230" class="l"> 230: </a>            }
<a href="#231" id="231" class="l"> 231: </a>            <span class="php-keyword1">elseif</span> (<span class="php-var">$this</span>-&gt;config-&gt;item(<span class="php-quote">'rest_ip_whitelist_enabled'</span>))
<a href="#232" id="232" class="l"> 232: </a>            {
<a href="#233" id="233" class="l"> 233: </a>                <span class="php-var">$this</span>-&gt;_check_whitelist_auth();
<a href="#234" id="234" class="l"> 234: </a>            }
<a href="#235" id="235" class="l"> 235: </a>        }
<a href="#236" id="236" class="l"> 236: </a>
<a href="#237" id="237" class="l"> 237: </a>        <span class="php-comment">// Load DB if its enabled</span>
<a href="#238" id="238" class="l"> 238: </a>        <span class="php-keyword1">if</span> (config_item(<span class="php-quote">'rest_database_group'</span>) <span class="php-keyword1">AND</span> (config_item(<span class="php-quote">'rest_enable_keys'</span>) <span class="php-keyword1">OR</span> config_item(<span class="php-quote">'rest_enable_logging'</span>)))
<a href="#239" id="239" class="l"> 239: </a>        {
<a href="#240" id="240" class="l"> 240: </a>            <span class="php-var">$this</span>-&gt;rest-&gt;db = <span class="php-var">$this</span>-&gt;load-&gt;database(config_item(<span class="php-quote">'rest_database_group'</span>), <span class="php-keyword1">TRUE</span>);
<a href="#241" id="241" class="l"> 241: </a>        }
<a href="#242" id="242" class="l"> 242: </a>
<a href="#243" id="243" class="l"> 243: </a>        <span class="php-comment">// Use whatever database is in use (isset returns false)</span>
<a href="#244" id="244" class="l"> 244: </a>        <span class="php-keyword1">elseif</span> (@<span class="php-var">$this</span>-&gt;db)
<a href="#245" id="245" class="l"> 245: </a>        {
<a href="#246" id="246" class="l"> 246: </a>            <span class="php-var">$this</span>-&gt;rest-&gt;db = <span class="php-var">$this</span>-&gt;db;
<a href="#247" id="247" class="l"> 247: </a>        }
<a href="#248" id="248" class="l"> 248: </a>
<a href="#249" id="249" class="l"> 249: </a>        <span class="php-comment">// Checking for keys? GET TO WORK!</span>
<a href="#250" id="250" class="l"> 250: </a>        <span class="php-keyword1">if</span> (config_item(<span class="php-quote">'rest_enable_keys'</span>))
<a href="#251" id="251" class="l"> 251: </a>        {
<a href="#252" id="252" class="l"> 252: </a>            <span class="php-var">$this</span>-&gt;_allow = <span class="php-var">$this</span>-&gt;_detect_api_key();
<a href="#253" id="253" class="l"> 253: </a>        }
<a href="#254" id="254" class="l"> 254: </a>
<a href="#255" id="255" class="l"> 255: </a>        <span class="php-comment">// only allow ajax requests</span>
<a href="#256" id="256" class="l"> 256: </a>        <span class="php-keyword1">if</span> ( ! <span class="php-var">$this</span>-&gt;input-&gt;is_ajax_request() <span class="php-keyword1">AND</span> config_item(<span class="php-quote">'rest_ajax_only'</span>))
<a href="#257" id="257" class="l"> 257: </a>        {
<a href="#258" id="258" class="l"> 258: </a>            <span class="php-var">$this</span>-&gt;response(<span class="php-keyword1">array</span>(<span class="php-quote">'status'</span> =&gt; <span class="php-keyword1">false</span>, <span class="php-quote">'error'</span> =&gt; <span class="php-quote">'Only AJAX requests are accepted.'</span>), <span class="php-num">505</span>);
<a href="#259" id="259" class="l"> 259: </a>        }
<a href="#260" id="260" class="l"> 260: </a>    }
<a href="#261" id="261" class="l"> 261: </a>
<a href="#262" id="262" class="l"> 262: </a>    <span class="php-comment">/**
</span><a href="#263" id="263" class="l"> 263: </a><span class="php-comment">     * Remap
</span><a href="#264" id="264" class="l"> 264: </a><span class="php-comment">     *
</span><a href="#265" id="265" class="l"> 265: </a><span class="php-comment">     * Requests are not made to methods directly, the request will be for 
</span><a href="#266" id="266" class="l"> 266: </a><span class="php-comment">     * an &quot;object&quot;. This simply maps the object and method to the correct 
</span><a href="#267" id="267" class="l"> 267: </a><span class="php-comment">     * Controller method.
</span><a href="#268" id="268" class="l"> 268: </a><span class="php-comment">     *
</span><a href="#269" id="269" class="l"> 269: </a><span class="php-comment">     * @param string $object_called
</span><a href="#270" id="270" class="l"> 270: </a><span class="php-comment">     * @param array $arguments The arguments passed to the controller method.
</span><a href="#271" id="271" class="l"> 271: </a><span class="php-comment">     */</span>
<a href="#272" id="272" class="l"> 272: </a>    <span class="php-keyword1">public</span> <span class="php-keyword1">function</span> _remap(<span class="php-var">$object_called</span>, <span class="php-var">$arguments</span>)
<a href="#273" id="273" class="l"> 273: </a>    {
<a href="#274" id="274" class="l"> 274: </a>        <span class="php-var">$pattern</span> = <span class="php-quote">'/^(.*)\.('</span>.<span class="php-keyword2">implode</span>(<span class="php-quote">'|'</span>, <span class="php-keyword2">array_keys</span>(<span class="php-var">$this</span>-&gt;_supported_formats)).<span class="php-quote">')$/'</span>;
<a href="#275" id="275" class="l"> 275: </a>        <span class="php-keyword1">if</span> (<span class="php-keyword2">preg_match</span>(<span class="php-var">$pattern</span>, <span class="php-var">$object_called</span>, <span class="php-var">$matches</span>))
<a href="#276" id="276" class="l"> 276: </a>        {
<a href="#277" id="277" class="l"> 277: </a>            <span class="php-var">$object_called</span> = <span class="php-var">$matches</span>[<span class="php-num">1</span>];
<a href="#278" id="278" class="l"> 278: </a>        }
<a href="#279" id="279" class="l"> 279: </a>
<a href="#280" id="280" class="l"> 280: </a>        <span class="php-var">$controller_method</span> = <span class="php-var">$object_called</span>.<span class="php-quote">'_'</span>.<span class="php-var">$this</span>-&gt;request-&gt;method;
<a href="#281" id="281" class="l"> 281: </a>
<a href="#282" id="282" class="l"> 282: </a>        <span class="php-comment">// Do we want to log this method (if allowed by config)?</span>
<a href="#283" id="283" class="l"> 283: </a>        <span class="php-var">$log_method</span> = !(<span class="php-keyword1">isset</span>(<span class="php-var">$this</span>-&gt;methods[<span class="php-var">$controller_method</span>][<span class="php-quote">'log'</span>]) <span class="php-keyword1">AND</span> <span class="php-var">$this</span>-&gt;methods[<span class="php-var">$controller_method</span>][<span class="php-quote">'log'</span>] == <span class="php-keyword1">FALSE</span>);
<a href="#284" id="284" class="l"> 284: </a>
<a href="#285" id="285" class="l"> 285: </a>        <span class="php-comment">// Use keys for this method?</span>
<a href="#286" id="286" class="l"> 286: </a>        <span class="php-var">$use_key</span> = !(<span class="php-keyword1">isset</span>(<span class="php-var">$this</span>-&gt;methods[<span class="php-var">$controller_method</span>][<span class="php-quote">'key'</span>]) <span class="php-keyword1">AND</span> <span class="php-var">$this</span>-&gt;methods[<span class="php-var">$controller_method</span>][<span class="php-quote">'key'</span>] == <span class="php-keyword1">FALSE</span>);
<a href="#287" id="287" class="l"> 287: </a>
<a href="#288" id="288" class="l"> 288: </a>        <span class="php-comment">// Get that useless shitty key out of here</span>
<a href="#289" id="289" class="l"> 289: </a>        <span class="php-keyword1">if</span> (config_item(<span class="php-quote">'rest_enable_keys'</span>) <span class="php-keyword1">AND</span> <span class="php-var">$use_key</span> <span class="php-keyword1">AND</span> <span class="php-var">$this</span>-&gt;_allow === <span class="php-keyword1">FALSE</span>)
<a href="#290" id="290" class="l"> 290: </a>        {
<a href="#291" id="291" class="l"> 291: </a>            <span class="php-keyword1">if</span> (config_item(<span class="php-quote">'rest_enable_logging'</span>) <span class="php-keyword1">AND</span> <span class="php-var">$log_method</span>)
<a href="#292" id="292" class="l"> 292: </a>            {
<a href="#293" id="293" class="l"> 293: </a>                <span class="php-var">$this</span>-&gt;_log_request();
<a href="#294" id="294" class="l"> 294: </a>            }
<a href="#295" id="295" class="l"> 295: </a>
<a href="#296" id="296" class="l"> 296: </a>            <span class="php-var">$this</span>-&gt;response(<span class="php-keyword1">array</span>(<span class="php-quote">'status'</span> =&gt; <span class="php-keyword1">false</span>, <span class="php-quote">'error'</span> =&gt; <span class="php-quote">'Invalid API Key.'</span>), <span class="php-num">403</span>);
<a href="#297" id="297" class="l"> 297: </a>        }
<a href="#298" id="298" class="l"> 298: </a>
<a href="#299" id="299" class="l"> 299: </a>        <span class="php-comment">// Sure it exists, but can they do anything with it?</span>
<a href="#300" id="300" class="l"> 300: </a>        <span class="php-keyword1">if</span> ( ! <span class="php-keyword2">method_exists</span>(<span class="php-var">$this</span>, <span class="php-var">$controller_method</span>))
<a href="#301" id="301" class="l"> 301: </a>        {
<a href="#302" id="302" class="l"> 302: </a>            <span class="php-var">$this</span>-&gt;response(<span class="php-keyword1">array</span>(<span class="php-quote">'status'</span> =&gt; <span class="php-keyword1">false</span>, <span class="php-quote">'error'</span> =&gt; <span class="php-quote">'Unknown method.'</span>), <span class="php-num">404</span>);
<a href="#303" id="303" class="l"> 303: </a>        }
<a href="#304" id="304" class="l"> 304: </a>
<a href="#305" id="305" class="l"> 305: </a>        <span class="php-comment">// Doing key related stuff? Can only do it if they have a key right?</span>
<a href="#306" id="306" class="l"> 306: </a>        <span class="php-keyword1">if</span> (config_item(<span class="php-quote">'rest_enable_keys'</span>) <span class="php-keyword1">AND</span> !<span class="php-keyword1">empty</span>(<span class="php-var">$this</span>-&gt;rest-&gt;<span class="php-keyword2">key</span>))
<a href="#307" id="307" class="l"> 307: </a>        {
<a href="#308" id="308" class="l"> 308: </a>            <span class="php-comment">// Check the limit</span>
<a href="#309" id="309" class="l"> 309: </a>            <span class="php-keyword1">if</span> (config_item(<span class="php-quote">'rest_enable_limits'</span>) <span class="php-keyword1">AND</span> !<span class="php-var">$this</span>-&gt;_check_limit(<span class="php-var">$controller_method</span>))
<a href="#310" id="310" class="l"> 310: </a>            {
<a href="#311" id="311" class="l"> 311: </a>                <span class="php-var">$this</span>-&gt;response(<span class="php-keyword1">array</span>(<span class="php-quote">'status'</span> =&gt; <span class="php-keyword1">false</span>, <span class="php-quote">'error'</span> =&gt; <span class="php-quote">'This API key has reached the hourly limit for this method.'</span>), <span class="php-num">401</span>);
<a href="#312" id="312" class="l"> 312: </a>            }
<a href="#313" id="313" class="l"> 313: </a>
<a href="#314" id="314" class="l"> 314: </a>            <span class="php-comment">// If no level is set use 0, they probably aren't using permissions</span>
<a href="#315" id="315" class="l"> 315: </a>            <span class="php-var">$level</span> = <span class="php-keyword1">isset</span>(<span class="php-var">$this</span>-&gt;methods[<span class="php-var">$controller_method</span>][<span class="php-quote">'level'</span>]) ? <span class="php-var">$this</span>-&gt;methods[<span class="php-var">$controller_method</span>][<span class="php-quote">'level'</span>] : <span class="php-num">0</span>;
<a href="#316" id="316" class="l"> 316: </a>
<a href="#317" id="317" class="l"> 317: </a>            <span class="php-comment">// If no level is set, or it is lower than/equal to the key's level</span>
<a href="#318" id="318" class="l"> 318: </a>            <span class="php-var">$authorized</span> = <span class="php-var">$level</span> &lt;= <span class="php-var">$this</span>-&gt;rest-&gt;level;
<a href="#319" id="319" class="l"> 319: </a>
<a href="#320" id="320" class="l"> 320: </a>            <span class="php-comment">// IM TELLIN!</span>
<a href="#321" id="321" class="l"> 321: </a>            <span class="php-keyword1">if</span> (config_item(<span class="php-quote">'rest_enable_logging'</span>) <span class="php-keyword1">AND</span> <span class="php-var">$log_method</span>)
<a href="#322" id="322" class="l"> 322: </a>            {
<a href="#323" id="323" class="l"> 323: </a>                <span class="php-var">$this</span>-&gt;_log_request(<span class="php-var">$authorized</span>);
<a href="#324" id="324" class="l"> 324: </a>            }
<a href="#325" id="325" class="l"> 325: </a>
<a href="#326" id="326" class="l"> 326: </a>            <span class="php-comment">// They don't have good enough perms</span>
<a href="#327" id="327" class="l"> 327: </a>            <span class="php-var">$authorized</span> <span class="php-keyword1">OR</span> <span class="php-var">$this</span>-&gt;response(<span class="php-keyword1">array</span>(<span class="php-quote">'status'</span> =&gt; <span class="php-keyword1">false</span>, <span class="php-quote">'error'</span> =&gt; <span class="php-quote">'This API key does not have enough permissions.'</span>), <span class="php-num">401</span>);
<a href="#328" id="328" class="l"> 328: </a>        }
<a href="#329" id="329" class="l"> 329: </a>
<a href="#330" id="330" class="l"> 330: </a>        <span class="php-comment">// No key stuff, but record that stuff is happening</span>
<a href="#331" id="331" class="l"> 331: </a>        <span class="php-keyword1">else</span> <span class="php-keyword1">if</span> (config_item(<span class="php-quote">'rest_enable_logging'</span>) <span class="php-keyword1">AND</span> <span class="php-var">$log_method</span>)
<a href="#332" id="332" class="l"> 332: </a>        {
<a href="#333" id="333" class="l"> 333: </a>            <span class="php-var">$this</span>-&gt;_log_request(<span class="php-var">$authorized</span> = <span class="php-keyword1">TRUE</span>);
<a href="#334" id="334" class="l"> 334: </a>        }
<a href="#335" id="335" class="l"> 335: </a>
<a href="#336" id="336" class="l"> 336: </a>        <span class="php-comment">// And...... GO!</span>
<a href="#337" id="337" class="l"> 337: </a>        <span class="php-keyword2">call_user_func_array</span>(<span class="php-keyword1">array</span>(<span class="php-var">$this</span>, <span class="php-var">$controller_method</span>), <span class="php-var">$arguments</span>);
<a href="#338" id="338" class="l"> 338: </a>    }
<a href="#339" id="339" class="l"> 339: </a>
<a href="#340" id="340" class="l"> 340: </a>    <span class="php-comment">/**
</span><a href="#341" id="341" class="l"> 341: </a><span class="php-comment">     * Response
</span><a href="#342" id="342" class="l"> 342: </a><span class="php-comment">     *
</span><a href="#343" id="343" class="l"> 343: </a><span class="php-comment">     * Takes pure data and optionally a status code, then creates the response.
</span><a href="#344" id="344" class="l"> 344: </a><span class="php-comment">     *
</span><a href="#345" id="345" class="l"> 345: </a><span class="php-comment">     * @param array $data
</span><a href="#346" id="346" class="l"> 346: </a><span class="php-comment">     * @param null|int $http_code 
</span><a href="#347" id="347" class="l"> 347: </a><span class="php-comment">     */</span>
<a href="#348" id="348" class="l"> 348: </a>    <span class="php-keyword1">public</span> <span class="php-keyword1">function</span> response(<span class="php-var">$data</span> = <span class="php-keyword1">array</span>(), <span class="php-var">$http_code</span> = <span class="php-keyword1">null</span>)
<a href="#349" id="349" class="l"> 349: </a>    {
<a href="#350" id="350" class="l"> 350: </a>        <span class="php-keyword1">global</span> <span class="php-var">$CFG</span>;
<a href="#351" id="351" class="l"> 351: </a>
<a href="#352" id="352" class="l"> 352: </a>        <span class="php-comment">// If data is empty and not code provide, error and bail</span>
<a href="#353" id="353" class="l"> 353: </a>        <span class="php-keyword1">if</span> (<span class="php-keyword1">empty</span>(<span class="php-var">$data</span>) &amp;&amp; <span class="php-var">$http_code</span> === <span class="php-keyword1">null</span>)
<a href="#354" id="354" class="l"> 354: </a>        {
<a href="#355" id="355" class="l"> 355: </a>            <span class="php-var">$http_code</span> = <span class="php-num">404</span>;
<a href="#356" id="356" class="l"> 356: </a>
<a href="#357" id="357" class="l"> 357: </a>            <span class="php-comment">//create the output variable here in the case of $this-&gt;response(array());</span>
<a href="#358" id="358" class="l"> 358: </a>            <span class="php-var">$output</span> = <span class="php-var">$data</span>;
<a href="#359" id="359" class="l"> 359: </a>        }
<a href="#360" id="360" class="l"> 360: </a>
<a href="#361" id="361" class="l"> 361: </a>        <span class="php-comment">// Otherwise (if no data but 200 provided) or some data, carry on camping!</span>
<a href="#362" id="362" class="l"> 362: </a>        <span class="php-keyword1">else</span>
<a href="#363" id="363" class="l"> 363: </a>        {
<a href="#364" id="364" class="l"> 364: </a>            <span class="php-comment">// Is compression requested?</span>
<a href="#365" id="365" class="l"> 365: </a>            <span class="php-keyword1">if</span> (<span class="php-var">$CFG</span>-&gt;item(<span class="php-quote">'compress_output'</span>) === <span class="php-keyword1">TRUE</span> &amp;&amp; <span class="php-var">$this</span>-&gt;_zlib_oc == <span class="php-keyword1">FALSE</span>)
<a href="#366" id="366" class="l"> 366: </a>            {
<a href="#367" id="367" class="l"> 367: </a>                <span class="php-keyword1">if</span> (<span class="php-keyword2">extension_loaded</span>(<span class="php-quote">'zlib'</span>))
<a href="#368" id="368" class="l"> 368: </a>                {
<a href="#369" id="369" class="l"> 369: </a>                    <span class="php-keyword1">if</span> (<span class="php-keyword1">isset</span>(<span class="php-var">$_SERVER</span>[<span class="php-quote">'HTTP_ACCEPT_ENCODING'</span>]) <span class="php-keyword1">AND</span> <span class="php-keyword2">strpos</span>(<span class="php-var">$_SERVER</span>[<span class="php-quote">'HTTP_ACCEPT_ENCODING'</span>], <span class="php-quote">'gzip'</span>) !== <span class="php-keyword1">FALSE</span>)
<a href="#370" id="370" class="l"> 370: </a>                    {
<a href="#371" id="371" class="l"> 371: </a>                        <span class="php-keyword2">ob_start</span>(<span class="php-quote">'ob_gzhandler'</span>);
<a href="#372" id="372" class="l"> 372: </a>                    }
<a href="#373" id="373" class="l"> 373: </a>                }
<a href="#374" id="374" class="l"> 374: </a>            }
<a href="#375" id="375" class="l"> 375: </a>            
<a href="#376" id="376" class="l"> 376: </a>            <span class="php-keyword2">is_numeric</span>(<span class="php-var">$http_code</span>) <span class="php-keyword1">OR</span> <span class="php-var">$http_code</span> = <span class="php-num">200</span>;
<a href="#377" id="377" class="l"> 377: </a>
<a href="#378" id="378" class="l"> 378: </a>            <span class="php-comment">// If the format method exists, call and return the output in that format</span>
<a href="#379" id="379" class="l"> 379: </a>            <span class="php-keyword1">if</span> (<span class="php-keyword2">method_exists</span>(<span class="php-var">$this</span>, <span class="php-quote">'_format_'</span>.<span class="php-var">$this</span>-&gt;response-&gt;format))
<a href="#380" id="380" class="l"> 380: </a>            {
<a href="#381" id="381" class="l"> 381: </a>                <span class="php-comment">// Set the correct format header</span>
<a href="#382" id="382" class="l"> 382: </a>                <span class="php-keyword2">header</span>(<span class="php-quote">'Content-Type: '</span>.<span class="php-var">$this</span>-&gt;_supported_formats[<span class="php-var">$this</span>-&gt;response-&gt;format]);
<a href="#383" id="383" class="l"> 383: </a>
<a href="#384" id="384" class="l"> 384: </a>                <span class="php-var">$output</span> = <span class="php-var">$this</span>-&gt;{<span class="php-quote">'_format_'</span>.<span class="php-var">$this</span>-&gt;response-&gt;format}(<span class="php-var">$data</span>);
<a href="#385" id="385" class="l"> 385: </a>            }
<a href="#386" id="386" class="l"> 386: </a>
<a href="#387" id="387" class="l"> 387: </a>            <span class="php-comment">// If the format method exists, call and return the output in that format</span>
<a href="#388" id="388" class="l"> 388: </a>            <span class="php-keyword1">elseif</span> (<span class="php-keyword2">method_exists</span>(<span class="php-var">$this</span>-&gt;format, <span class="php-quote">'to_'</span>.<span class="php-var">$this</span>-&gt;response-&gt;format))
<a href="#389" id="389" class="l"> 389: </a>            {
<a href="#390" id="390" class="l"> 390: </a>                <span class="php-comment">// Set the correct format header</span>
<a href="#391" id="391" class="l"> 391: </a>                <span class="php-keyword2">header</span>(<span class="php-quote">'Content-Type: '</span>.<span class="php-var">$this</span>-&gt;_supported_formats[<span class="php-var">$this</span>-&gt;response-&gt;format]);
<a href="#392" id="392" class="l"> 392: </a>
<a href="#393" id="393" class="l"> 393: </a>                <span class="php-var">$output</span> = <span class="php-var">$this</span>-&gt;format-&gt;factory(<span class="php-var">$data</span>)-&gt;{<span class="php-quote">'to_'</span>.<span class="php-var">$this</span>-&gt;response-&gt;format}();
<a href="#394" id="394" class="l"> 394: </a>            }
<a href="#395" id="395" class="l"> 395: </a>
<a href="#396" id="396" class="l"> 396: </a>            <span class="php-comment">// Format not supported, output directly</span>
<a href="#397" id="397" class="l"> 397: </a>            <span class="php-keyword1">else</span>
<a href="#398" id="398" class="l"> 398: </a>            {
<a href="#399" id="399" class="l"> 399: </a>                <span class="php-var">$output</span> = <span class="php-var">$data</span>;
<a href="#400" id="400" class="l"> 400: </a>            }
<a href="#401" id="401" class="l"> 401: </a>        }
<a href="#402" id="402" class="l"> 402: </a>
<a href="#403" id="403" class="l"> 403: </a>        <span class="php-keyword2">header</span>(<span class="php-quote">'HTTP/1.1: '</span> . <span class="php-var">$http_code</span>);
<a href="#404" id="404" class="l"> 404: </a>        <span class="php-keyword2">header</span>(<span class="php-quote">'Status: '</span> . <span class="php-var">$http_code</span>);
<a href="#405" id="405" class="l"> 405: </a>
<a href="#406" id="406" class="l"> 406: </a>        <span class="php-comment">// If zlib.output_compression is enabled it will compress the output,</span>
<a href="#407" id="407" class="l"> 407: </a>        <span class="php-comment">// but it will not modify the content-length header to compensate for</span>
<a href="#408" id="408" class="l"> 408: </a>        <span class="php-comment">// the reduction, causing the browser to hang waiting for more data.</span>
<a href="#409" id="409" class="l"> 409: </a>        <span class="php-comment">// We'll just skip content-length in those cases.</span>
<a href="#410" id="410" class="l"> 410: </a>        <span class="php-keyword1">if</span> ( ! <span class="php-var">$this</span>-&gt;_zlib_oc &amp;&amp; ! <span class="php-var">$CFG</span>-&gt;item(<span class="php-quote">'compress_output'</span>))
<a href="#411" id="411" class="l"> 411: </a>        {
<a href="#412" id="412" class="l"> 412: </a>            <span class="php-keyword2">header</span>(<span class="php-quote">'Content-Length: '</span> . <span class="php-keyword2">strlen</span>(<span class="php-var">$output</span>));
<a href="#413" id="413" class="l"> 413: </a>        }
<a href="#414" id="414" class="l"> 414: </a>
<a href="#415" id="415" class="l"> 415: </a>        <span class="php-keyword1">exit</span>(<span class="php-var">$output</span>);
<a href="#416" id="416" class="l"> 416: </a>    }
<a href="#417" id="417" class="l"> 417: </a>
<a href="#418" id="418" class="l"> 418: </a>    <span class="php-comment">/*
</span><a href="#419" id="419" class="l"> 419: </a><span class="php-comment">     * Detect input format
</span><a href="#420" id="420" class="l"> 420: </a><span class="php-comment">     *
</span><a href="#421" id="421" class="l"> 421: </a><span class="php-comment">     * Detect which format the HTTP Body is provided in
</span><a href="#422" id="422" class="l"> 422: </a><span class="php-comment">     */</span>
<a href="#423" id="423" class="l"> 423: </a>    <span class="php-keyword1">protected</span> <span class="php-keyword1">function</span> _detect_input_format()
<a href="#424" id="424" class="l"> 424: </a>    {
<a href="#425" id="425" class="l"> 425: </a>        <span class="php-keyword1">if</span> (<span class="php-var">$this</span>-&gt;input-&gt;server(<span class="php-quote">'CONTENT_TYPE'</span>))
<a href="#426" id="426" class="l"> 426: </a>        {
<a href="#427" id="427" class="l"> 427: </a>            <span class="php-comment">// Check all formats against the HTTP_ACCEPT header</span>
<a href="#428" id="428" class="l"> 428: </a>            <span class="php-keyword1">foreach</span> (<span class="php-var">$this</span>-&gt;_supported_formats <span class="php-keyword1">as</span> <span class="php-var">$format</span> =&gt; <span class="php-var">$mime</span>)
<a href="#429" id="429" class="l"> 429: </a>            {
<a href="#430" id="430" class="l"> 430: </a>                <span class="php-keyword1">if</span> (<span class="php-keyword2">strpos</span>(<span class="php-var">$match</span> = <span class="php-var">$this</span>-&gt;input-&gt;server(<span class="php-quote">'CONTENT_TYPE'</span>), <span class="php-quote">';'</span>))
<a href="#431" id="431" class="l"> 431: </a>                {
<a href="#432" id="432" class="l"> 432: </a>                    <span class="php-var">$match</span> = <span class="php-keyword2">current</span>(<span class="php-keyword2">explode</span>(<span class="php-quote">';'</span>, <span class="php-var">$match</span>));
<a href="#433" id="433" class="l"> 433: </a>                }
<a href="#434" id="434" class="l"> 434: </a>
<a href="#435" id="435" class="l"> 435: </a>                <span class="php-keyword1">if</span> (<span class="php-var">$match</span> == <span class="php-var">$mime</span>)
<a href="#436" id="436" class="l"> 436: </a>                {
<a href="#437" id="437" class="l"> 437: </a>                    <span class="php-keyword1">return</span> <span class="php-var">$format</span>;
<a href="#438" id="438" class="l"> 438: </a>                }
<a href="#439" id="439" class="l"> 439: </a>            }
<a href="#440" id="440" class="l"> 440: </a>        }
<a href="#441" id="441" class="l"> 441: </a>
<a href="#442" id="442" class="l"> 442: </a>        <span class="php-keyword1">return</span> <span class="php-keyword1">NULL</span>;
<a href="#443" id="443" class="l"> 443: </a>    }
<a href="#444" id="444" class="l"> 444: </a>
<a href="#445" id="445" class="l"> 445: </a>    <span class="php-comment">/**
</span><a href="#446" id="446" class="l"> 446: </a><span class="php-comment">     * Detect format
</span><a href="#447" id="447" class="l"> 447: </a><span class="php-comment">     *
</span><a href="#448" id="448" class="l"> 448: </a><span class="php-comment">     * Detect which format should be used to output the data.
</span><a href="#449" id="449" class="l"> 449: </a><span class="php-comment">     * 
</span><a href="#450" id="450" class="l"> 450: </a><span class="php-comment">     * @return string The output format. 
</span><a href="#451" id="451" class="l"> 451: </a><span class="php-comment">     */</span>
<a href="#452" id="452" class="l"> 452: </a>    <span class="php-keyword1">protected</span> <span class="php-keyword1">function</span> _detect_output_format()
<a href="#453" id="453" class="l"> 453: </a>    {
<a href="#454" id="454" class="l"> 454: </a>        <span class="php-var">$pattern</span> = <span class="php-quote">'/\.('</span>.<span class="php-keyword2">implode</span>(<span class="php-quote">'|'</span>, <span class="php-keyword2">array_keys</span>(<span class="php-var">$this</span>-&gt;_supported_formats)).<span class="php-quote">')$/'</span>;
<a href="#455" id="455" class="l"> 455: </a>
<a href="#456" id="456" class="l"> 456: </a>        <span class="php-comment">// Check if a file extension is used</span>
<a href="#457" id="457" class="l"> 457: </a>        <span class="php-keyword1">if</span> (<span class="php-keyword2">preg_match</span>(<span class="php-var">$pattern</span>, <span class="php-var">$this</span>-&gt;uri-&gt;uri_string(), <span class="php-var">$matches</span>))
<a href="#458" id="458" class="l"> 458: </a>        {
<a href="#459" id="459" class="l"> 459: </a>            <span class="php-keyword1">return</span> <span class="php-var">$matches</span>[<span class="php-num">1</span>];
<a href="#460" id="460" class="l"> 460: </a>        }
<a href="#461" id="461" class="l"> 461: </a>
<a href="#462" id="462" class="l"> 462: </a>        <span class="php-comment">// Check if a file extension is used</span>
<a href="#463" id="463" class="l"> 463: </a>        <span class="php-keyword1">elseif</span> (<span class="php-var">$this</span>-&gt;_get_args <span class="php-keyword1">AND</span> !<span class="php-keyword2">is_array</span>(<span class="php-keyword2">end</span>(<span class="php-var">$this</span>-&gt;_get_args)) <span class="php-keyword1">AND</span> <span class="php-keyword2">preg_match</span>(<span class="php-var">$pattern</span>, <span class="php-keyword2">end</span>(<span class="php-var">$this</span>-&gt;_get_args), <span class="php-var">$matches</span>))
<a href="#464" id="464" class="l"> 464: </a>        {
<a href="#465" id="465" class="l"> 465: </a>            <span class="php-comment">// The key of the last argument</span>
<a href="#466" id="466" class="l"> 466: </a>            <span class="php-var">$last_key</span> = <span class="php-keyword2">end</span>(<span class="php-keyword2">array_keys</span>(<span class="php-var">$this</span>-&gt;_get_args));
<a href="#467" id="467" class="l"> 467: </a>
<a href="#468" id="468" class="l"> 468: </a>            <span class="php-comment">// Remove the extension from arguments too</span>
<a href="#469" id="469" class="l"> 469: </a>            <span class="php-var">$this</span>-&gt;_get_args[<span class="php-var">$last_key</span>] = <span class="php-keyword2">preg_replace</span>(<span class="php-var">$pattern</span>, <span class="php-quote">''</span>, <span class="php-var">$this</span>-&gt;_get_args[<span class="php-var">$last_key</span>]);
<a href="#470" id="470" class="l"> 470: </a>            <span class="php-var">$this</span>-&gt;_args[<span class="php-var">$last_key</span>] = <span class="php-keyword2">preg_replace</span>(<span class="php-var">$pattern</span>, <span class="php-quote">''</span>, <span class="php-var">$this</span>-&gt;_args[<span class="php-var">$last_key</span>]);
<a href="#471" id="471" class="l"> 471: </a>
<a href="#472" id="472" class="l"> 472: </a>            <span class="php-keyword1">return</span> <span class="php-var">$matches</span>[<span class="php-num">1</span>];
<a href="#473" id="473" class="l"> 473: </a>        }
<a href="#474" id="474" class="l"> 474: </a>
<a href="#475" id="475" class="l"> 475: </a>        <span class="php-comment">// A format has been passed as an argument in the URL and it is supported</span>
<a href="#476" id="476" class="l"> 476: </a>        <span class="php-keyword1">if</span> (<span class="php-keyword1">isset</span>(<span class="php-var">$this</span>-&gt;_get_args[<span class="php-quote">'format'</span>]) <span class="php-keyword1">AND</span> <span class="php-keyword2">array_key_exists</span>(<span class="php-var">$this</span>-&gt;_get_args[<span class="php-quote">'format'</span>], <span class="php-var">$this</span>-&gt;_supported_formats))
<a href="#477" id="477" class="l"> 477: </a>        {
<a href="#478" id="478" class="l"> 478: </a>            <span class="php-keyword1">return</span> <span class="php-var">$this</span>-&gt;_get_args[<span class="php-quote">'format'</span>];
<a href="#479" id="479" class="l"> 479: </a>        }
<a href="#480" id="480" class="l"> 480: </a>
<a href="#481" id="481" class="l"> 481: </a>        <span class="php-comment">// Otherwise, check the HTTP_ACCEPT (if it exists and we are allowed)</span>
<a href="#482" id="482" class="l"> 482: </a>        <span class="php-keyword1">if</span> (<span class="php-var">$this</span>-&gt;config-&gt;item(<span class="php-quote">'rest_ignore_http_accept'</span>) === <span class="php-keyword1">FALSE</span> <span class="php-keyword1">AND</span> <span class="php-var">$this</span>-&gt;input-&gt;server(<span class="php-quote">'HTTP_ACCEPT'</span>))
<a href="#483" id="483" class="l"> 483: </a>        {
<a href="#484" id="484" class="l"> 484: </a>            <span class="php-comment">// Check all formats against the HTTP_ACCEPT header</span>
<a href="#485" id="485" class="l"> 485: </a>            <span class="php-keyword1">foreach</span> (<span class="php-keyword2">array_keys</span>(<span class="php-var">$this</span>-&gt;_supported_formats) <span class="php-keyword1">as</span> <span class="php-var">$format</span>)
<a href="#486" id="486" class="l"> 486: </a>            {
<a href="#487" id="487" class="l"> 487: </a>                <span class="php-comment">// Has this format been requested?</span>
<a href="#488" id="488" class="l"> 488: </a>                <span class="php-keyword1">if</span> (<span class="php-keyword2">strpos</span>(<span class="php-var">$this</span>-&gt;input-&gt;server(<span class="php-quote">'HTTP_ACCEPT'</span>), <span class="php-var">$format</span>) !== <span class="php-keyword1">FALSE</span>)
<a href="#489" id="489" class="l"> 489: </a>                {
<a href="#490" id="490" class="l"> 490: </a>                    <span class="php-comment">// If not HTML or XML assume its right and send it on its way</span>
<a href="#491" id="491" class="l"> 491: </a>                    <span class="php-keyword1">if</span> (<span class="php-var">$format</span> != <span class="php-quote">'html'</span> <span class="php-keyword1">AND</span> <span class="php-var">$format</span> != <span class="php-quote">'xml'</span>)
<a href="#492" id="492" class="l"> 492: </a>                    {
<a href="#493" id="493" class="l"> 493: </a>
<a href="#494" id="494" class="l"> 494: </a>                        <span class="php-keyword1">return</span> <span class="php-var">$format</span>;
<a href="#495" id="495" class="l"> 495: </a>                    }
<a href="#496" id="496" class="l"> 496: </a>
<a href="#497" id="497" class="l"> 497: </a>                    <span class="php-comment">// HTML or XML have shown up as a match</span>
<a href="#498" id="498" class="l"> 498: </a>                    <span class="php-keyword1">else</span>
<a href="#499" id="499" class="l"> 499: </a>                    {
<a href="#500" id="500" class="l"> 500: </a>                        <span class="php-comment">// If it is truely HTML, it wont want any XML</span>
<a href="#501" id="501" class="l"> 501: </a>                        <span class="php-keyword1">if</span> (<span class="php-var">$format</span> == <span class="php-quote">'html'</span> <span class="php-keyword1">AND</span> <span class="php-keyword2">strpos</span>(<span class="php-var">$this</span>-&gt;input-&gt;server(<span class="php-quote">'HTTP_ACCEPT'</span>), <span class="php-quote">'xml'</span>) === <span class="php-keyword1">FALSE</span>)
<a href="#502" id="502" class="l"> 502: </a>                        {
<a href="#503" id="503" class="l"> 503: </a>                            <span class="php-keyword1">return</span> <span class="php-var">$format</span>;
<a href="#504" id="504" class="l"> 504: </a>                        }
<a href="#505" id="505" class="l"> 505: </a>
<a href="#506" id="506" class="l"> 506: </a>                        <span class="php-comment">// If it is truely XML, it wont want any HTML</span>
<a href="#507" id="507" class="l"> 507: </a>                        <span class="php-keyword1">elseif</span> (<span class="php-var">$format</span> == <span class="php-quote">'xml'</span> <span class="php-keyword1">AND</span> <span class="php-keyword2">strpos</span>(<span class="php-var">$this</span>-&gt;input-&gt;server(<span class="php-quote">'HTTP_ACCEPT'</span>), <span class="php-quote">'html'</span>) === <span class="php-keyword1">FALSE</span>)
<a href="#508" id="508" class="l"> 508: </a>                        {
<a href="#509" id="509" class="l"> 509: </a>                            <span class="php-keyword1">return</span> <span class="php-var">$format</span>;
<a href="#510" id="510" class="l"> 510: </a>                        }
<a href="#511" id="511" class="l"> 511: </a>                    }
<a href="#512" id="512" class="l"> 512: </a>                }
<a href="#513" id="513" class="l"> 513: </a>            }
<a href="#514" id="514" class="l"> 514: </a>        } <span class="php-comment">// End HTTP_ACCEPT checking</span>
<a href="#515" id="515" class="l"> 515: </a>
<a href="#516" id="516" class="l"> 516: </a>        <span class="php-comment">// Well, none of that has worked! Let's see if the controller has a default</span>
<a href="#517" id="517" class="l"> 517: </a>        <span class="php-keyword1">if</span> ( ! <span class="php-keyword1">empty</span>(<span class="php-var">$this</span>-&gt;rest_format))
<a href="#518" id="518" class="l"> 518: </a>        {
<a href="#519" id="519" class="l"> 519: </a>            <span class="php-keyword1">return</span> <span class="php-var">$this</span>-&gt;rest_format;
<a href="#520" id="520" class="l"> 520: </a>        }
<a href="#521" id="521" class="l"> 521: </a>
<a href="#522" id="522" class="l"> 522: </a>        <span class="php-comment">// Just use the default format</span>
<a href="#523" id="523" class="l"> 523: </a>        <span class="php-keyword1">return</span> config_item(<span class="php-quote">'rest_default_format'</span>);
<a href="#524" id="524" class="l"> 524: </a>    }
<a href="#525" id="525" class="l"> 525: </a>
<a href="#526" id="526" class="l"> 526: </a>    <span class="php-comment">/**
</span><a href="#527" id="527" class="l"> 527: </a><span class="php-comment">     * Detect method
</span><a href="#528" id="528" class="l"> 528: </a><span class="php-comment">     *
</span><a href="#529" id="529" class="l"> 529: </a><span class="php-comment">     * Detect which method (POST, PUT, GET, DELETE) is being used
</span><a href="#530" id="530" class="l"> 530: </a><span class="php-comment">     * 
</span><a href="#531" id="531" class="l"> 531: </a><span class="php-comment">     * @return string 
</span><a href="#532" id="532" class="l"> 532: </a><span class="php-comment">     */</span>
<a href="#533" id="533" class="l"> 533: </a>    <span class="php-keyword1">protected</span> <span class="php-keyword1">function</span> _detect_method()
<a href="#534" id="534" class="l"> 534: </a>    {
<a href="#535" id="535" class="l"> 535: </a>        <span class="php-var">$method</span> = <span class="php-keyword2">strtolower</span>(<span class="php-var">$this</span>-&gt;input-&gt;server(<span class="php-quote">'REQUEST_METHOD'</span>));
<a href="#536" id="536" class="l"> 536: </a>        
<a href="#537" id="537" class="l"> 537: </a>        <span class="php-keyword1">if</span> (<span class="php-var">$this</span>-&gt;config-&gt;item(<span class="php-quote">'enable_emulate_request'</span>))
<a href="#538" id="538" class="l"> 538: </a>        {
<a href="#539" id="539" class="l"> 539: </a>            <span class="php-keyword1">if</span> (<span class="php-var">$this</span>-&gt;input-&gt;post(<span class="php-quote">'_method'</span>))
<a href="#540" id="540" class="l"> 540: </a>            {
<a href="#541" id="541" class="l"> 541: </a>                <span class="php-var">$method</span> = <span class="php-keyword2">strtolower</span>(<span class="php-var">$this</span>-&gt;input-&gt;post(<span class="php-quote">'_method'</span>));
<a href="#542" id="542" class="l"> 542: </a>            }
<a href="#543" id="543" class="l"> 543: </a>                <span class="php-keyword1">else</span> <span class="php-keyword1">if</span> (<span class="php-var">$this</span>-&gt;input-&gt;server(<span class="php-quote">'HTTP_X_HTTP_METHOD_OVERRIDE'</span>))
<a href="#544" id="544" class="l"> 544: </a>                {
<a href="#545" id="545" class="l"> 545: </a>                    <span class="php-var">$method</span> = <span class="php-keyword2">strtolower</span>(<span class="php-var">$this</span>-&gt;input-&gt;server(<span class="php-quote">'HTTP_X_HTTP_METHOD_OVERRIDE'</span>));
<a href="#546" id="546" class="l"> 546: </a>                }           
<a href="#547" id="547" class="l"> 547: </a>        }
<a href="#548" id="548" class="l"> 548: </a>
<a href="#549" id="549" class="l"> 549: </a>        <span class="php-keyword1">if</span> (<span class="php-keyword2">in_array</span>(<span class="php-var">$method</span>, <span class="php-keyword1">array</span>(<span class="php-quote">'get'</span>, <span class="php-quote">'delete'</span>, <span class="php-quote">'post'</span>, <span class="php-quote">'put'</span>)))
<a href="#550" id="550" class="l"> 550: </a>        {
<a href="#551" id="551" class="l"> 551: </a>            <span class="php-keyword1">return</span> <span class="php-var">$method</span>;
<a href="#552" id="552" class="l"> 552: </a>        }
<a href="#553" id="553" class="l"> 553: </a>
<a href="#554" id="554" class="l"> 554: </a>        <span class="php-keyword1">return</span> <span class="php-quote">'get'</span>;
<a href="#555" id="555" class="l"> 555: </a>    }
<a href="#556" id="556" class="l"> 556: </a>
<a href="#557" id="557" class="l"> 557: </a>    <span class="php-comment">/**
</span><a href="#558" id="558" class="l"> 558: </a><span class="php-comment">     * Detect API Key
</span><a href="#559" id="559" class="l"> 559: </a><span class="php-comment">     *
</span><a href="#560" id="560" class="l"> 560: </a><span class="php-comment">     * See if the user has provided an API key
</span><a href="#561" id="561" class="l"> 561: </a><span class="php-comment">     * 
</span><a href="#562" id="562" class="l"> 562: </a><span class="php-comment">     * @return boolean 
</span><a href="#563" id="563" class="l"> 563: </a><span class="php-comment">     */</span>
<a href="#564" id="564" class="l"> 564: </a>    <span class="php-keyword1">protected</span> <span class="php-keyword1">function</span> _detect_api_key()
<a href="#565" id="565" class="l"> 565: </a>    {
<a href="#566" id="566" class="l"> 566: </a>        <span class="php-comment">// Get the api key name variable set in the rest config file</span>
<a href="#567" id="567" class="l"> 567: </a>        <span class="php-var">$api_key_variable</span> = config_item(<span class="php-quote">'rest_key_name'</span>);
<a href="#568" id="568" class="l"> 568: </a>
<a href="#569" id="569" class="l"> 569: </a>        <span class="php-comment">// Work out the name of the SERVER entry based on config</span>
<a href="#570" id="570" class="l"> 570: </a>        <span class="php-var">$key_name</span> = <span class="php-quote">'HTTP_'</span>.<span class="php-keyword2">strtoupper</span>(<span class="php-keyword2">str_replace</span>(<span class="php-quote">'-'</span>, <span class="php-quote">'_'</span>, <span class="php-var">$api_key_variable</span>));
<a href="#571" id="571" class="l"> 571: </a>
<a href="#572" id="572" class="l"> 572: </a>        <span class="php-var">$this</span>-&gt;rest-&gt;<span class="php-keyword2">key</span> = <span class="php-keyword1">NULL</span>;
<a href="#573" id="573" class="l"> 573: </a>        <span class="php-var">$this</span>-&gt;rest-&gt;level = <span class="php-keyword1">NULL</span>;
<a href="#574" id="574" class="l"> 574: </a>        <span class="php-var">$this</span>-&gt;rest-&gt;user_id = <span class="php-keyword1">NULL</span>;
<a href="#575" id="575" class="l"> 575: </a>        <span class="php-var">$this</span>-&gt;rest-&gt;ignore_limits = <span class="php-keyword1">FALSE</span>;
<a href="#576" id="576" class="l"> 576: </a>
<a href="#577" id="577" class="l"> 577: </a>        <span class="php-comment">// Find the key from server or arguments</span>
<a href="#578" id="578" class="l"> 578: </a>        <span class="php-keyword1">if</span> ((<span class="php-var">$key</span> = <span class="php-keyword1">isset</span>(<span class="php-var">$this</span>-&gt;_args[<span class="php-var">$api_key_variable</span>]) ? <span class="php-var">$this</span>-&gt;_args[<span class="php-var">$api_key_variable</span>] : <span class="php-var">$this</span>-&gt;input-&gt;server(<span class="php-var">$key_name</span>)))
<a href="#579" id="579" class="l"> 579: </a>        {
<a href="#580" id="580" class="l"> 580: </a>            <span class="php-keyword1">if</span> ( ! (<span class="php-var">$row</span> = <span class="php-var">$this</span>-&gt;rest-&gt;db-&gt;where(<span class="php-quote">'key'</span>, <span class="php-var">$key</span>)-&gt;get(config_item(<span class="php-quote">'rest_keys_table'</span>))-&gt;row()))
<a href="#581" id="581" class="l"> 581: </a>            {
<a href="#582" id="582" class="l"> 582: </a>                <span class="php-keyword1">return</span> <span class="php-keyword1">FALSE</span>;
<a href="#583" id="583" class="l"> 583: </a>            }
<a href="#584" id="584" class="l"> 584: </a>
<a href="#585" id="585" class="l"> 585: </a>            <span class="php-var">$this</span>-&gt;rest-&gt;<span class="php-keyword2">key</span> = <span class="php-var">$row</span>-&gt;<span class="php-keyword2">key</span>;
<a href="#586" id="586" class="l"> 586: </a>
<a href="#587" id="587" class="l"> 587: </a>            <span class="php-keyword1">isset</span>(<span class="php-var">$row</span>-&gt;user_id) <span class="php-keyword1">AND</span> <span class="php-var">$this</span>-&gt;rest-&gt;user_id = <span class="php-var">$row</span>-&gt;user_id;
<a href="#588" id="588" class="l"> 588: </a>            <span class="php-keyword1">isset</span>(<span class="php-var">$row</span>-&gt;level) <span class="php-keyword1">AND</span> <span class="php-var">$this</span>-&gt;rest-&gt;level = <span class="php-var">$row</span>-&gt;level;
<a href="#589" id="589" class="l"> 589: </a>            <span class="php-keyword1">isset</span>(<span class="php-var">$row</span>-&gt;ignore_limits) <span class="php-keyword1">AND</span> <span class="php-var">$this</span>-&gt;rest-&gt;ignore_limits = <span class="php-var">$row</span>-&gt;ignore_limits;
<a href="#590" id="590" class="l"> 590: </a>
<a href="#591" id="591" class="l"> 591: </a>            <span class="php-keyword1">return</span> <span class="php-keyword1">TRUE</span>;
<a href="#592" id="592" class="l"> 592: </a>        }
<a href="#593" id="593" class="l"> 593: </a>
<a href="#594" id="594" class="l"> 594: </a>        <span class="php-comment">// No key has been sent</span>
<a href="#595" id="595" class="l"> 595: </a>        <span class="php-keyword1">return</span> <span class="php-keyword1">FALSE</span>;
<a href="#596" id="596" class="l"> 596: </a>    }
<a href="#597" id="597" class="l"> 597: </a>
<a href="#598" id="598" class="l"> 598: </a>    <span class="php-comment">/**
</span><a href="#599" id="599" class="l"> 599: </a><span class="php-comment">     * Detect language(s)
</span><a href="#600" id="600" class="l"> 600: </a><span class="php-comment">     *
</span><a href="#601" id="601" class="l"> 601: </a><span class="php-comment">     * What language do they want it in?
</span><a href="#602" id="602" class="l"> 602: </a><span class="php-comment">     * 
</span><a href="#603" id="603" class="l"> 603: </a><span class="php-comment">     * @return null|string The language code.
</span><a href="#604" id="604" class="l"> 604: </a><span class="php-comment">     */</span>
<a href="#605" id="605" class="l"> 605: </a>    <span class="php-keyword1">protected</span> <span class="php-keyword1">function</span> _detect_lang()
<a href="#606" id="606" class="l"> 606: </a>    {
<a href="#607" id="607" class="l"> 607: </a>        <span class="php-keyword1">if</span> ( ! <span class="php-var">$lang</span> = <span class="php-var">$this</span>-&gt;input-&gt;server(<span class="php-quote">'HTTP_ACCEPT_LANGUAGE'</span>))
<a href="#608" id="608" class="l"> 608: </a>        {
<a href="#609" id="609" class="l"> 609: </a>            <span class="php-keyword1">return</span> <span class="php-keyword1">NULL</span>;
<a href="#610" id="610" class="l"> 610: </a>        }
<a href="#611" id="611" class="l"> 611: </a>
<a href="#612" id="612" class="l"> 612: </a>        <span class="php-comment">// They might have sent a few, make it an array</span>
<a href="#613" id="613" class="l"> 613: </a>        <span class="php-keyword1">if</span> (<span class="php-keyword2">strpos</span>(<span class="php-var">$lang</span>, <span class="php-quote">','</span>) !== <span class="php-keyword1">FALSE</span>)
<a href="#614" id="614" class="l"> 614: </a>        {
<a href="#615" id="615" class="l"> 615: </a>            <span class="php-var">$langs</span> = <span class="php-keyword2">explode</span>(<span class="php-quote">','</span>, <span class="php-var">$lang</span>);
<a href="#616" id="616" class="l"> 616: </a>
<a href="#617" id="617" class="l"> 617: </a>            <span class="php-var">$return_langs</span> = <span class="php-keyword1">array</span>();
<a href="#618" id="618" class="l"> 618: </a>            <span class="php-var">$i</span> = <span class="php-num">1</span>;
<a href="#619" id="619" class="l"> 619: </a>            <span class="php-keyword1">foreach</span> (<span class="php-var">$langs</span> <span class="php-keyword1">as</span> <span class="php-var">$lang</span>)
<a href="#620" id="620" class="l"> 620: </a>            {
<a href="#621" id="621" class="l"> 621: </a>                <span class="php-comment">// Remove weight and strip space</span>
<a href="#622" id="622" class="l"> 622: </a>                <span class="php-keyword1">list</span>(<span class="php-var">$lang</span>) = <span class="php-keyword2">explode</span>(<span class="php-quote">';'</span>, <span class="php-var">$lang</span>);
<a href="#623" id="623" class="l"> 623: </a>                <span class="php-var">$return_langs</span>[] = <span class="php-keyword2">trim</span>(<span class="php-var">$lang</span>);
<a href="#624" id="624" class="l"> 624: </a>            }
<a href="#625" id="625" class="l"> 625: </a>
<a href="#626" id="626" class="l"> 626: </a>            <span class="php-keyword1">return</span> <span class="php-var">$return_langs</span>;
<a href="#627" id="627" class="l"> 627: </a>        }
<a href="#628" id="628" class="l"> 628: </a>
<a href="#629" id="629" class="l"> 629: </a>        <span class="php-comment">// Nope, just return the string</span>
<a href="#630" id="630" class="l"> 630: </a>        <span class="php-keyword1">return</span> <span class="php-var">$lang</span>;
<a href="#631" id="631" class="l"> 631: </a>    }
<a href="#632" id="632" class="l"> 632: </a>
<a href="#633" id="633" class="l"> 633: </a>    <span class="php-comment">/**
</span><a href="#634" id="634" class="l"> 634: </a><span class="php-comment">     * Log request
</span><a href="#635" id="635" class="l"> 635: </a><span class="php-comment">     *
</span><a href="#636" id="636" class="l"> 636: </a><span class="php-comment">     * Record the entry for awesomeness purposes
</span><a href="#637" id="637" class="l"> 637: </a><span class="php-comment">     * 
</span><a href="#638" id="638" class="l"> 638: </a><span class="php-comment">     * @param boolean $authorized
</span><a href="#639" id="639" class="l"> 639: </a><span class="php-comment">     * @return object 
</span><a href="#640" id="640" class="l"> 640: </a><span class="php-comment">     */</span>
<a href="#641" id="641" class="l"> 641: </a>    <span class="php-keyword1">protected</span> <span class="php-keyword1">function</span> _log_request(<span class="php-var">$authorized</span> = <span class="php-keyword1">FALSE</span>)
<a href="#642" id="642" class="l"> 642: </a>    {
<a href="#643" id="643" class="l"> 643: </a>        <span class="php-keyword1">return</span> <span class="php-var">$this</span>-&gt;rest-&gt;db-&gt;insert(config_item(<span class="php-quote">'rest_logs_table'</span>), <span class="php-keyword1">array</span>(
<a href="#644" id="644" class="l"> 644: </a>                    <span class="php-quote">'uri'</span> =&gt; <span class="php-var">$this</span>-&gt;uri-&gt;uri_string(),
<a href="#645" id="645" class="l"> 645: </a>                    <span class="php-quote">'method'</span> =&gt; <span class="php-var">$this</span>-&gt;request-&gt;method,
<a href="#646" id="646" class="l"> 646: </a>                    <span class="php-quote">'params'</span> =&gt; <span class="php-var">$this</span>-&gt;_args ? <span class="php-keyword2">serialize</span>(<span class="php-var">$this</span>-&gt;_args) : <span class="php-keyword1">null</span>,
<a href="#647" id="647" class="l"> 647: </a>                    <span class="php-quote">'api_key'</span> =&gt; <span class="php-keyword1">isset</span>(<span class="php-var">$this</span>-&gt;rest-&gt;<span class="php-keyword2">key</span>) ? <span class="php-var">$this</span>-&gt;rest-&gt;<span class="php-keyword2">key</span> : <span class="php-quote">''</span>,
<a href="#648" id="648" class="l"> 648: </a>                    <span class="php-quote">'ip_address'</span> =&gt; <span class="php-var">$this</span>-&gt;input-&gt;ip_address(),
<a href="#649" id="649" class="l"> 649: </a>                    <span class="php-quote">'time'</span> =&gt; <span class="php-keyword2">function_exists</span>(<span class="php-quote">'now'</span>) ? now() : <span class="php-keyword2">time</span>(),
<a href="#650" id="650" class="l"> 650: </a>                    <span class="php-quote">'authorized'</span> =&gt; <span class="php-var">$authorized</span>
<a href="#651" id="651" class="l"> 651: </a>                ));
<a href="#652" id="652" class="l"> 652: </a>    }
<a href="#653" id="653" class="l"> 653: </a>
<a href="#654" id="654" class="l"> 654: </a>    <span class="php-comment">/**
</span><a href="#655" id="655" class="l"> 655: </a><span class="php-comment">     * Limiting requests
</span><a href="#656" id="656" class="l"> 656: </a><span class="php-comment">     *
</span><a href="#657" id="657" class="l"> 657: </a><span class="php-comment">     * Check if the requests are coming in a tad too fast.
</span><a href="#658" id="658" class="l"> 658: </a><span class="php-comment">     *
</span><a href="#659" id="659" class="l"> 659: </a><span class="php-comment">     * @param string $controller_method The method deing called.
</span><a href="#660" id="660" class="l"> 660: </a><span class="php-comment">     * @return boolean 
</span><a href="#661" id="661" class="l"> 661: </a><span class="php-comment">     */</span>
<a href="#662" id="662" class="l"> 662: </a>    <span class="php-keyword1">protected</span> <span class="php-keyword1">function</span> _check_limit(<span class="php-var">$controller_method</span>)
<a href="#663" id="663" class="l"> 663: </a>    {
<a href="#664" id="664" class="l"> 664: </a>        <span class="php-comment">// They are special, or it might not even have a limit</span>
<a href="#665" id="665" class="l"> 665: </a>        <span class="php-keyword1">if</span> ( ! <span class="php-keyword1">empty</span>(<span class="php-var">$this</span>-&gt;rest-&gt;ignore_limits) <span class="php-keyword1">OR</span> !<span class="php-keyword1">isset</span>(<span class="php-var">$this</span>-&gt;methods[<span class="php-var">$controller_method</span>][<span class="php-quote">'limit'</span>]))
<a href="#666" id="666" class="l"> 666: </a>        {
<a href="#667" id="667" class="l"> 667: </a>            <span class="php-comment">// On your way sonny-jim.</span>
<a href="#668" id="668" class="l"> 668: </a>            <span class="php-keyword1">return</span> <span class="php-keyword1">TRUE</span>;
<a href="#669" id="669" class="l"> 669: </a>        }
<a href="#670" id="670" class="l"> 670: </a>
<a href="#671" id="671" class="l"> 671: </a>        <span class="php-comment">// How many times can you get to this method an hour?</span>
<a href="#672" id="672" class="l"> 672: </a>        <span class="php-var">$limit</span> = <span class="php-var">$this</span>-&gt;methods[<span class="php-var">$controller_method</span>][<span class="php-quote">'limit'</span>];
<a href="#673" id="673" class="l"> 673: </a>
<a href="#674" id="674" class="l"> 674: </a>        <span class="php-comment">// Get data on a keys usage</span>
<a href="#675" id="675" class="l"> 675: </a>        <span class="php-var">$result</span> = <span class="php-var">$this</span>-&gt;rest-&gt;db
<a href="#676" id="676" class="l"> 676: </a>                -&gt;where(<span class="php-quote">'uri'</span>, <span class="php-var">$this</span>-&gt;uri-&gt;uri_string())
<a href="#677" id="677" class="l"> 677: </a>                -&gt;where(<span class="php-quote">'api_key'</span>, <span class="php-var">$this</span>-&gt;rest-&gt;<span class="php-keyword2">key</span>)
<a href="#678" id="678" class="l"> 678: </a>                -&gt;get(config_item(<span class="php-quote">'rest_limits_table'</span>))
<a href="#679" id="679" class="l"> 679: </a>                -&gt;row();
<a href="#680" id="680" class="l"> 680: </a>
<a href="#681" id="681" class="l"> 681: </a>        <span class="php-comment">// No calls yet, or been an hour since they called</span>
<a href="#682" id="682" class="l"> 682: </a>        <span class="php-keyword1">if</span> ( ! <span class="php-var">$result</span> <span class="php-keyword1">OR</span> <span class="php-var">$result</span>-&gt;hour_started &lt; <span class="php-keyword2">time</span>() - (<span class="php-num">60</span> * <span class="php-num">60</span>))
<a href="#683" id="683" class="l"> 683: </a>        {
<a href="#684" id="684" class="l"> 684: </a>            <span class="php-comment">// Right, set one up from scratch</span>
<a href="#685" id="685" class="l"> 685: </a>            <span class="php-var">$this</span>-&gt;rest-&gt;db-&gt;insert(config_item(<span class="php-quote">'rest_limits_table'</span>), <span class="php-keyword1">array</span>(
<a href="#686" id="686" class="l"> 686: </a>                <span class="php-quote">'uri'</span> =&gt; <span class="php-var">$this</span>-&gt;uri-&gt;uri_string(),
<a href="#687" id="687" class="l"> 687: </a>                <span class="php-quote">'api_key'</span> =&gt; <span class="php-keyword1">isset</span>(<span class="php-var">$this</span>-&gt;rest-&gt;<span class="php-keyword2">key</span>) ? <span class="php-var">$this</span>-&gt;rest-&gt;<span class="php-keyword2">key</span> : <span class="php-quote">''</span>,
<a href="#688" id="688" class="l"> 688: </a>                <span class="php-quote">'count'</span> =&gt; <span class="php-num">1</span>,
<a href="#689" id="689" class="l"> 689: </a>                <span class="php-quote">'hour_started'</span> =&gt; <span class="php-keyword2">time</span>()
<a href="#690" id="690" class="l"> 690: </a>            ));
<a href="#691" id="691" class="l"> 691: </a>        }
<a href="#692" id="692" class="l"> 692: </a>
<a href="#693" id="693" class="l"> 693: </a>        <span class="php-comment">// They have called within the hour, so lets update</span>
<a href="#694" id="694" class="l"> 694: </a>        <span class="php-keyword1">else</span>
<a href="#695" id="695" class="l"> 695: </a>        {
<a href="#696" id="696" class="l"> 696: </a>            <span class="php-comment">// Your luck is out, you've called too many times!</span>
<a href="#697" id="697" class="l"> 697: </a>            <span class="php-keyword1">if</span> (<span class="php-var">$result</span>-&gt;<span class="php-keyword2">count</span> &gt;= <span class="php-var">$limit</span>)
<a href="#698" id="698" class="l"> 698: </a>            {
<a href="#699" id="699" class="l"> 699: </a>                <span class="php-keyword1">return</span> <span class="php-keyword1">FALSE</span>;
<a href="#700" id="700" class="l"> 700: </a>            }
<a href="#701" id="701" class="l"> 701: </a>
<a href="#702" id="702" class="l"> 702: </a>            <span class="php-var">$this</span>-&gt;rest-&gt;db
<a href="#703" id="703" class="l"> 703: </a>                    -&gt;where(<span class="php-quote">'uri'</span>, <span class="php-var">$this</span>-&gt;uri-&gt;uri_string())
<a href="#704" id="704" class="l"> 704: </a>                    -&gt;where(<span class="php-quote">'api_key'</span>, <span class="php-var">$this</span>-&gt;rest-&gt;<span class="php-keyword2">key</span>)
<a href="#705" id="705" class="l"> 705: </a>                    -&gt;set(<span class="php-quote">'count'</span>, <span class="php-quote">'count + 1'</span>, <span class="php-keyword1">FALSE</span>)
<a href="#706" id="706" class="l"> 706: </a>                    -&gt;update(config_item(<span class="php-quote">'rest_limits_table'</span>));
<a href="#707" id="707" class="l"> 707: </a>        }
<a href="#708" id="708" class="l"> 708: </a>
<a href="#709" id="709" class="l"> 709: </a>        <span class="php-keyword1">return</span> <span class="php-keyword1">TRUE</span>;
<a href="#710" id="710" class="l"> 710: </a>    }
<a href="#711" id="711" class="l"> 711: </a>
<a href="#712" id="712" class="l"> 712: </a>    <span class="php-comment">/**
</span><a href="#713" id="713" class="l"> 713: </a><span class="php-comment">     * Auth override check
</span><a href="#714" id="714" class="l"> 714: </a><span class="php-comment">     *
</span><a href="#715" id="715" class="l"> 715: </a><span class="php-comment">     * Check if there is a specific auth type set for the current class/method 
</span><a href="#716" id="716" class="l"> 716: </a><span class="php-comment">     * being called.
</span><a href="#717" id="717" class="l"> 717: </a><span class="php-comment">     *
</span><a href="#718" id="718" class="l"> 718: </a><span class="php-comment">     * @return boolean 
</span><a href="#719" id="719" class="l"> 719: </a><span class="php-comment">     */</span>
<a href="#720" id="720" class="l"> 720: </a>    <span class="php-keyword1">protected</span> <span class="php-keyword1">function</span> _auth_override_check()
<a href="#721" id="721" class="l"> 721: </a>    {
<a href="#722" id="722" class="l"> 722: </a>
<a href="#723" id="723" class="l"> 723: </a>        <span class="php-comment">// Assign the class/method auth type override array from the config</span>
<a href="#724" id="724" class="l"> 724: </a>        <span class="php-var">$this</span>-&gt;overrides_array = <span class="php-var">$this</span>-&gt;config-&gt;item(<span class="php-quote">'auth_override_class_method'</span>);
<a href="#725" id="725" class="l"> 725: </a>
<a href="#726" id="726" class="l"> 726: </a>        <span class="php-comment">// Check to see if the override array is even populated, otherwise return false</span>
<a href="#727" id="727" class="l"> 727: </a>        <span class="php-keyword1">if</span> (<span class="php-keyword1">empty</span>(<span class="php-var">$this</span>-&gt;overrides_array))
<a href="#728" id="728" class="l"> 728: </a>        {
<a href="#729" id="729" class="l"> 729: </a>            <span class="php-keyword1">return</span> <span class="php-keyword1">false</span>;
<a href="#730" id="730" class="l"> 730: </a>        }
<a href="#731" id="731" class="l"> 731: </a>
<a href="#732" id="732" class="l"> 732: </a>        <span class="php-comment">// Check to see if there's an override value set for the current class/method being called</span>
<a href="#733" id="733" class="l"> 733: </a>        <span class="php-keyword1">if</span> (<span class="php-keyword1">empty</span>(<span class="php-var">$this</span>-&gt;overrides_array[<span class="php-var">$this</span>-&gt;router-&gt;<span class="php-keyword1">class</span>][<span class="php-var">$this</span>-&gt;router-&gt;method]))
<a href="#734" id="734" class="l"> 734: </a>        {
<a href="#735" id="735" class="l"> 735: </a>            <span class="php-keyword1">return</span> <span class="php-keyword1">false</span>;
<a href="#736" id="736" class="l"> 736: </a>        }
<a href="#737" id="737" class="l"> 737: </a>
<a href="#738" id="738" class="l"> 738: </a>        <span class="php-comment">// None auth override found, prepare nothing but send back a true override flag</span>
<a href="#739" id="739" class="l"> 739: </a>        <span class="php-keyword1">if</span> (<span class="php-var">$this</span>-&gt;overrides_array[<span class="php-var">$this</span>-&gt;router-&gt;<span class="php-keyword1">class</span>][<span class="php-var">$this</span>-&gt;router-&gt;method] == <span class="php-quote">'none'</span>)
<a href="#740" id="740" class="l"> 740: </a>        {
<a href="#741" id="741" class="l"> 741: </a>            <span class="php-keyword1">return</span> <span class="php-keyword1">true</span>;
<a href="#742" id="742" class="l"> 742: </a>        }
<a href="#743" id="743" class="l"> 743: </a>
<a href="#744" id="744" class="l"> 744: </a>        <span class="php-comment">// Basic auth override found, prepare basic</span>
<a href="#745" id="745" class="l"> 745: </a>        <span class="php-keyword1">if</span> (<span class="php-var">$this</span>-&gt;overrides_array[<span class="php-var">$this</span>-&gt;router-&gt;<span class="php-keyword1">class</span>][<span class="php-var">$this</span>-&gt;router-&gt;method] == <span class="php-quote">'basic'</span>)
<a href="#746" id="746" class="l"> 746: </a>        {
<a href="#747" id="747" class="l"> 747: </a>            <span class="php-var">$this</span>-&gt;_prepare_basic_auth();
<a href="#748" id="748" class="l"> 748: </a>            <span class="php-keyword1">return</span> <span class="php-keyword1">true</span>;
<a href="#749" id="749" class="l"> 749: </a>        }
<a href="#750" id="750" class="l"> 750: </a>
<a href="#751" id="751" class="l"> 751: </a>        <span class="php-comment">// Digest auth override found, prepare digest</span>
<a href="#752" id="752" class="l"> 752: </a>        <span class="php-keyword1">if</span> (<span class="php-var">$this</span>-&gt;overrides_array[<span class="php-var">$this</span>-&gt;router-&gt;<span class="php-keyword1">class</span>][<span class="php-var">$this</span>-&gt;router-&gt;method] == <span class="php-quote">'digest'</span>)
<a href="#753" id="753" class="l"> 753: </a>        {
<a href="#754" id="754" class="l"> 754: </a>            <span class="php-var">$this</span>-&gt;_prepare_digest_auth();
<a href="#755" id="755" class="l"> 755: </a>            <span class="php-keyword1">return</span> <span class="php-keyword1">true</span>;
<a href="#756" id="756" class="l"> 756: </a>        }
<a href="#757" id="757" class="l"> 757: </a>
<a href="#758" id="758" class="l"> 758: </a>        <span class="php-comment">// Whitelist auth override found, check client's ip against config whitelist</span>
<a href="#759" id="759" class="l"> 759: </a>        <span class="php-keyword1">if</span> (<span class="php-var">$this</span>-&gt;overrides_array[<span class="php-var">$this</span>-&gt;router-&gt;<span class="php-keyword1">class</span>][<span class="php-var">$this</span>-&gt;router-&gt;method] == <span class="php-quote">'whitelist'</span>)
<a href="#760" id="760" class="l"> 760: </a>        {
<a href="#761" id="761" class="l"> 761: </a>            <span class="php-var">$this</span>-&gt;_check_whitelist_auth();
<a href="#762" id="762" class="l"> 762: </a>            <span class="php-keyword1">return</span> <span class="php-keyword1">true</span>;
<a href="#763" id="763" class="l"> 763: </a>        }
<a href="#764" id="764" class="l"> 764: </a>
<a href="#765" id="765" class="l"> 765: </a>        <span class="php-comment">// Return false when there is an override value set but it does not match </span>
<a href="#766" id="766" class="l"> 766: </a>        <span class="php-comment">// 'basic', 'digest', or 'none'. (the value was misspelled)</span>
<a href="#767" id="767" class="l"> 767: </a>        <span class="php-keyword1">return</span> <span class="php-keyword1">false</span>;
<a href="#768" id="768" class="l"> 768: </a>    }
<a href="#769" id="769" class="l"> 769: </a>
<a href="#770" id="770" class="l"> 770: </a>    <span class="php-comment">// INPUT FUNCTION --------------------------------------------------------------</span>
<a href="#771" id="771" class="l"> 771: </a>
<a href="#772" id="772" class="l"> 772: </a>    <span class="php-comment">/**
</span><a href="#773" id="773" class="l"> 773: </a><span class="php-comment">     * Retrieve a value from the GET request arguments.
</span><a href="#774" id="774" class="l"> 774: </a><span class="php-comment">     *
</span><a href="#775" id="775" class="l"> 775: </a><span class="php-comment">     * @param string $key The key for the GET request argument to retrieve
</span><a href="#776" id="776" class="l"> 776: </a><span class="php-comment">     * @param boolean $xss_clean Whether the value should be XSS cleaned or not.
</span><a href="#777" id="777" class="l"> 777: </a><span class="php-comment">     * @return string The GET argument value.
</span><a href="#778" id="778" class="l"> 778: </a><span class="php-comment">     */</span>
<a href="#779" id="779" class="l"> 779: </a>    <span class="php-keyword1">public</span> <span class="php-keyword1">function</span> get(<span class="php-var">$key</span> = <span class="php-keyword1">NULL</span>, <span class="php-var">$xss_clean</span> = <span class="php-keyword1">TRUE</span>)
<a href="#780" id="780" class="l"> 780: </a>    {
<a href="#781" id="781" class="l"> 781: </a>        <span class="php-keyword1">if</span> (<span class="php-var">$key</span> === <span class="php-keyword1">NULL</span>)
<a href="#782" id="782" class="l"> 782: </a>        {
<a href="#783" id="783" class="l"> 783: </a>            <span class="php-keyword1">return</span> <span class="php-var">$this</span>-&gt;_get_args;
<a href="#784" id="784" class="l"> 784: </a>        }
<a href="#785" id="785" class="l"> 785: </a>
<a href="#786" id="786" class="l"> 786: </a>        <span class="php-keyword1">return</span> <span class="php-keyword2">array_key_exists</span>(<span class="php-var">$key</span>, <span class="php-var">$this</span>-&gt;_get_args) ? <span class="php-var">$this</span>-&gt;_xss_clean(<span class="php-var">$this</span>-&gt;_get_args[<span class="php-var">$key</span>], <span class="php-var">$xss_clean</span>) : <span class="php-keyword1">FALSE</span>;
<a href="#787" id="787" class="l"> 787: </a>    }
<a href="#788" id="788" class="l"> 788: </a>
<a href="#789" id="789" class="l"> 789: </a>    <span class="php-comment">/**
</span><a href="#790" id="790" class="l"> 790: </a><span class="php-comment">     * Retrieve a value from the POST request arguments.
</span><a href="#791" id="791" class="l"> 791: </a><span class="php-comment">     *
</span><a href="#792" id="792" class="l"> 792: </a><span class="php-comment">     * @param string $key The key for the POST request argument to retrieve
</span><a href="#793" id="793" class="l"> 793: </a><span class="php-comment">     * @param boolean $xss_clean Whether the value should be XSS cleaned or not.
</span><a href="#794" id="794" class="l"> 794: </a><span class="php-comment">     * @return string The POST argument value.
</span><a href="#795" id="795" class="l"> 795: </a><span class="php-comment">     */</span>
<a href="#796" id="796" class="l"> 796: </a>    <span class="php-keyword1">public</span> <span class="php-keyword1">function</span> post(<span class="php-var">$key</span> = <span class="php-keyword1">NULL</span>, <span class="php-var">$xss_clean</span> = <span class="php-keyword1">TRUE</span>)
<a href="#797" id="797" class="l"> 797: </a>    {
<a href="#798" id="798" class="l"> 798: </a>        <span class="php-keyword1">if</span> (<span class="php-var">$key</span> === <span class="php-keyword1">NULL</span>)
<a href="#799" id="799" class="l"> 799: </a>        {
<a href="#800" id="800" class="l"> 800: </a>            <span class="php-keyword1">return</span> <span class="php-var">$this</span>-&gt;_post_args;
<a href="#801" id="801" class="l"> 801: </a>        }
<a href="#802" id="802" class="l"> 802: </a>
<a href="#803" id="803" class="l"> 803: </a>        <span class="php-keyword1">return</span> <span class="php-var">$this</span>-&gt;input-&gt;post(<span class="php-var">$key</span>, <span class="php-var">$xss_clean</span>);
<a href="#804" id="804" class="l"> 804: </a>    }
<a href="#805" id="805" class="l"> 805: </a>
<a href="#806" id="806" class="l"> 806: </a>    <span class="php-comment">/**
</span><a href="#807" id="807" class="l"> 807: </a><span class="php-comment">     * Retrieve a value from the PUT request arguments.
</span><a href="#808" id="808" class="l"> 808: </a><span class="php-comment">     *
</span><a href="#809" id="809" class="l"> 809: </a><span class="php-comment">     * @param string $key The key for the PUT request argument to retrieve
</span><a href="#810" id="810" class="l"> 810: </a><span class="php-comment">     * @param boolean $xss_clean Whether the value should be XSS cleaned or not.
</span><a href="#811" id="811" class="l"> 811: </a><span class="php-comment">     * @return string The PUT argument value.
</span><a href="#812" id="812" class="l"> 812: </a><span class="php-comment">     */</span>
<a href="#813" id="813" class="l"> 813: </a>    <span class="php-keyword1">public</span> <span class="php-keyword1">function</span> put(<span class="php-var">$key</span> = <span class="php-keyword1">NULL</span>, <span class="php-var">$xss_clean</span> = <span class="php-keyword1">TRUE</span>)
<a href="#814" id="814" class="l"> 814: </a>    {
<a href="#815" id="815" class="l"> 815: </a>        <span class="php-keyword1">if</span> (<span class="php-var">$key</span> === <span class="php-keyword1">NULL</span>)
<a href="#816" id="816" class="l"> 816: </a>        {
<a href="#817" id="817" class="l"> 817: </a>            <span class="php-keyword1">return</span> <span class="php-var">$this</span>-&gt;_put_args;
<a href="#818" id="818" class="l"> 818: </a>        }
<a href="#819" id="819" class="l"> 819: </a>
<a href="#820" id="820" class="l"> 820: </a>        <span class="php-keyword1">return</span> <span class="php-keyword2">array_key_exists</span>(<span class="php-var">$key</span>, <span class="php-var">$this</span>-&gt;_put_args) ? <span class="php-var">$this</span>-&gt;_xss_clean(<span class="php-var">$this</span>-&gt;_put_args[<span class="php-var">$key</span>], <span class="php-var">$xss_clean</span>) : <span class="php-keyword1">FALSE</span>;
<a href="#821" id="821" class="l"> 821: </a>    }
<a href="#822" id="822" class="l"> 822: </a>
<a href="#823" id="823" class="l"> 823: </a>    <span class="php-comment">/**
</span><a href="#824" id="824" class="l"> 824: </a><span class="php-comment">     * Retrieve a value from the DELETE request arguments.
</span><a href="#825" id="825" class="l"> 825: </a><span class="php-comment">     *
</span><a href="#826" id="826" class="l"> 826: </a><span class="php-comment">     * @param string $key The key for the DELETE request argument to retrieve
</span><a href="#827" id="827" class="l"> 827: </a><span class="php-comment">     * @param boolean $xss_clean Whether the value should be XSS cleaned or not.
</span><a href="#828" id="828" class="l"> 828: </a><span class="php-comment">     * @return string The DELETE argument value.
</span><a href="#829" id="829" class="l"> 829: </a><span class="php-comment">     */</span>
<a href="#830" id="830" class="l"> 830: </a>    <span class="php-keyword1">public</span> <span class="php-keyword1">function</span> <span class="php-keyword2">delete</span>(<span class="php-var">$key</span> = <span class="php-keyword1">NULL</span>, <span class="php-var">$xss_clean</span> = <span class="php-keyword1">TRUE</span>)
<a href="#831" id="831" class="l"> 831: </a>    {
<a href="#832" id="832" class="l"> 832: </a>        <span class="php-keyword1">if</span> (<span class="php-var">$key</span> === <span class="php-keyword1">NULL</span>)
<a href="#833" id="833" class="l"> 833: </a>        {
<a href="#834" id="834" class="l"> 834: </a>            <span class="php-keyword1">return</span> <span class="php-var">$this</span>-&gt;_delete_args;
<a href="#835" id="835" class="l"> 835: </a>        }
<a href="#836" id="836" class="l"> 836: </a>
<a href="#837" id="837" class="l"> 837: </a>        <span class="php-keyword1">return</span> <span class="php-keyword2">array_key_exists</span>(<span class="php-var">$key</span>, <span class="php-var">$this</span>-&gt;_delete_args) ? <span class="php-var">$this</span>-&gt;_xss_clean(<span class="php-var">$this</span>-&gt;_delete_args[<span class="php-var">$key</span>], <span class="php-var">$xss_clean</span>) : <span class="php-keyword1">FALSE</span>;
<a href="#838" id="838" class="l"> 838: </a>    }
<a href="#839" id="839" class="l"> 839: </a>
<a href="#840" id="840" class="l"> 840: </a>    <span class="php-comment">/**
</span><a href="#841" id="841" class="l"> 841: </a><span class="php-comment">     * Process to protect from XSS attacks.
</span><a href="#842" id="842" class="l"> 842: </a><span class="php-comment">     *
</span><a href="#843" id="843" class="l"> 843: </a><span class="php-comment">     * @param string $val The input.
</span><a href="#844" id="844" class="l"> 844: </a><span class="php-comment">     * @param boolean $process Do clean or note the input.
</span><a href="#845" id="845" class="l"> 845: </a><span class="php-comment">     * @return string
</span><a href="#846" id="846" class="l"> 846: </a><span class="php-comment">     */</span>
<a href="#847" id="847" class="l"> 847: </a>    <span class="php-keyword1">protected</span> <span class="php-keyword1">function</span> _xss_clean(<span class="php-var">$val</span>, <span class="php-var">$process</span>)
<a href="#848" id="848" class="l"> 848: </a>    {
<a href="#849" id="849" class="l"> 849: </a>        <span class="php-keyword1">if</span> (CI_VERSION &lt; <span class="php-num">2</span>)
<a href="#850" id="850" class="l"> 850: </a>        {
<a href="#851" id="851" class="l"> 851: </a>            <span class="php-keyword1">return</span> <span class="php-var">$process</span> ? <span class="php-var">$this</span>-&gt;input-&gt;xss_clean(<span class="php-var">$val</span>) : <span class="php-var">$val</span>;
<a href="#852" id="852" class="l"> 852: </a>        }
<a href="#853" id="853" class="l"> 853: </a>
<a href="#854" id="854" class="l"> 854: </a>        <span class="php-keyword1">return</span> <span class="php-var">$process</span> ? <span class="php-var">$this</span>-&gt;security-&gt;xss_clean(<span class="php-var">$val</span>) : <span class="php-var">$val</span>;
<a href="#855" id="855" class="l"> 855: </a>    }
<a href="#856" id="856" class="l"> 856: </a>
<a href="#857" id="857" class="l"> 857: </a>    <span class="php-comment">/**
</span><a href="#858" id="858" class="l"> 858: </a><span class="php-comment">     * Retrieve the validation errors.
</span><a href="#859" id="859" class="l"> 859: </a><span class="php-comment">     *
</span><a href="#860" id="860" class="l"> 860: </a><span class="php-comment">     * @return array 
</span><a href="#861" id="861" class="l"> 861: </a><span class="php-comment">     */</span>
<a href="#862" id="862" class="l"> 862: </a>    <span class="php-keyword1">public</span> <span class="php-keyword1">function</span> validation_errors()
<a href="#863" id="863" class="l"> 863: </a>    {
<a href="#864" id="864" class="l"> 864: </a>        <span class="php-var">$string</span> = <span class="php-keyword2">strip_tags</span>(<span class="php-var">$this</span>-&gt;form_validation-&gt;error_string());
<a href="#865" id="865" class="l"> 865: </a>
<a href="#866" id="866" class="l"> 866: </a>        <span class="php-keyword1">return</span> <span class="php-keyword2">explode</span>(<span class="php-quote">&quot;\n&quot;</span>, <span class="php-keyword2">trim</span>(<span class="php-var">$string</span>, <span class="php-quote">&quot;\n&quot;</span>));
<a href="#867" id="867" class="l"> 867: </a>    }
<a href="#868" id="868" class="l"> 868: </a>
<a href="#869" id="869" class="l"> 869: </a>    <span class="php-comment">// SECURITY FUNCTIONS ---------------------------------------------------------</span>
<a href="#870" id="870" class="l"> 870: </a>
<a href="#871" id="871" class="l"> 871: </a>    <span class="php-comment">/**
</span><a href="#872" id="872" class="l"> 872: </a><span class="php-comment">     * Check if the user is logged in.
</span><a href="#873" id="873" class="l"> 873: </a><span class="php-comment">     *
</span><a href="#874" id="874" class="l"> 874: </a><span class="php-comment">     * @param string $username The user's name
</span><a href="#875" id="875" class="l"> 875: </a><span class="php-comment">     * @param string $password The user's password
</span><a href="#876" id="876" class="l"> 876: </a><span class="php-comment">     * @return boolean 
</span><a href="#877" id="877" class="l"> 877: </a><span class="php-comment">     */</span>
<a href="#878" id="878" class="l"> 878: </a>    <span class="php-keyword1">protected</span> <span class="php-keyword1">function</span> _check_login(<span class="php-var">$username</span> = <span class="php-quote">''</span>, <span class="php-var">$password</span> = <span class="php-keyword1">NULL</span>)
<a href="#879" id="879" class="l"> 879: </a>    {
<a href="#880" id="880" class="l"> 880: </a>        <span class="php-keyword1">if</span> (<span class="php-keyword1">empty</span>(<span class="php-var">$username</span>))
<a href="#881" id="881" class="l"> 881: </a>        {
<a href="#882" id="882" class="l"> 882: </a>            <span class="php-keyword1">return</span> <span class="php-keyword1">FALSE</span>;
<a href="#883" id="883" class="l"> 883: </a>        }
<a href="#884" id="884" class="l"> 884: </a>
<a href="#885" id="885" class="l"> 885: </a>        <span class="php-var">$valid_logins</span> = &amp; <span class="php-var">$this</span>-&gt;config-&gt;item(<span class="php-quote">'rest_valid_logins'</span>);
<a href="#886" id="886" class="l"> 886: </a>
<a href="#887" id="887" class="l"> 887: </a>        <span class="php-keyword1">if</span> ( ! <span class="php-keyword2">array_key_exists</span>(<span class="php-var">$username</span>, <span class="php-var">$valid_logins</span>))
<a href="#888" id="888" class="l"> 888: </a>        {
<a href="#889" id="889" class="l"> 889: </a>            <span class="php-keyword1">return</span> <span class="php-keyword1">FALSE</span>;
<a href="#890" id="890" class="l"> 890: </a>        }
<a href="#891" id="891" class="l"> 891: </a>
<a href="#892" id="892" class="l"> 892: </a>        <span class="php-comment">// If actually NULL (not empty string) then do not check it</span>
<a href="#893" id="893" class="l"> 893: </a>        <span class="php-keyword1">if</span> (<span class="php-var">$password</span> !== <span class="php-keyword1">NULL</span> <span class="php-keyword1">AND</span> <span class="php-var">$valid_logins</span>[<span class="php-var">$username</span>] != <span class="php-var">$password</span>)
<a href="#894" id="894" class="l"> 894: </a>        {
<a href="#895" id="895" class="l"> 895: </a>            <span class="php-keyword1">return</span> <span class="php-keyword1">FALSE</span>;
<a href="#896" id="896" class="l"> 896: </a>        }
<a href="#897" id="897" class="l"> 897: </a>
<a href="#898" id="898" class="l"> 898: </a>        <span class="php-keyword1">return</span> <span class="php-keyword1">TRUE</span>;
<a href="#899" id="899" class="l"> 899: </a>    }
<a href="#900" id="900" class="l"> 900: </a>
<a href="#901" id="901" class="l"> 901: </a>    <span class="php-comment">/**
</span><a href="#902" id="902" class="l"> 902: </a><span class="php-comment">     * @todo document this. 
</span><a href="#903" id="903" class="l"> 903: </a><span class="php-comment">     */</span>
<a href="#904" id="904" class="l"> 904: </a>    <span class="php-keyword1">protected</span> <span class="php-keyword1">function</span> _prepare_basic_auth()
<a href="#905" id="905" class="l"> 905: </a>    {
<a href="#906" id="906" class="l"> 906: </a>        <span class="php-comment">// If whitelist is enabled it has the first chance to kick them out</span>
<a href="#907" id="907" class="l"> 907: </a>        <span class="php-keyword1">if</span> (config_item(<span class="php-quote">'rest_ip_whitelist_enabled'</span>))
<a href="#908" id="908" class="l"> 908: </a>        {
<a href="#909" id="909" class="l"> 909: </a>            <span class="php-var">$this</span>-&gt;_check_whitelist_auth();
<a href="#910" id="910" class="l"> 910: </a>        }
<a href="#911" id="911" class="l"> 911: </a>
<a href="#912" id="912" class="l"> 912: </a>        <span class="php-var">$username</span> = <span class="php-keyword1">NULL</span>;
<a href="#913" id="913" class="l"> 913: </a>        <span class="php-var">$password</span> = <span class="php-keyword1">NULL</span>;
<a href="#914" id="914" class="l"> 914: </a>
<a href="#915" id="915" class="l"> 915: </a>        <span class="php-comment">// mod_php</span>
<a href="#916" id="916" class="l"> 916: </a>        <span class="php-keyword1">if</span> (<span class="php-var">$this</span>-&gt;input-&gt;server(<span class="php-quote">'PHP_AUTH_USER'</span>))
<a href="#917" id="917" class="l"> 917: </a>        {
<a href="#918" id="918" class="l"> 918: </a>            <span class="php-var">$username</span> = <span class="php-var">$this</span>-&gt;input-&gt;server(<span class="php-quote">'PHP_AUTH_USER'</span>);
<a href="#919" id="919" class="l"> 919: </a>            <span class="php-var">$password</span> = <span class="php-var">$this</span>-&gt;input-&gt;server(<span class="php-quote">'PHP_AUTH_PW'</span>);
<a href="#920" id="920" class="l"> 920: </a>        }
<a href="#921" id="921" class="l"> 921: </a>
<a href="#922" id="922" class="l"> 922: </a>        <span class="php-comment">// most other servers</span>
<a href="#923" id="923" class="l"> 923: </a>        <span class="php-keyword1">elseif</span> (<span class="php-var">$this</span>-&gt;input-&gt;server(<span class="php-quote">'HTTP_AUTHENTICATION'</span>))
<a href="#924" id="924" class="l"> 924: </a>        {
<a href="#925" id="925" class="l"> 925: </a>            <span class="php-keyword1">if</span> (<span class="php-keyword2">strpos</span>(<span class="php-keyword2">strtolower</span>(<span class="php-var">$this</span>-&gt;input-&gt;server(<span class="php-quote">'HTTP_AUTHENTICATION'</span>)), <span class="php-quote">'basic'</span>) === <span class="php-num">0</span>)
<a href="#926" id="926" class="l"> 926: </a>            {
<a href="#927" id="927" class="l"> 927: </a>                <span class="php-keyword1">list</span>(<span class="php-var">$username</span>, <span class="php-var">$password</span>) = <span class="php-keyword2">explode</span>(<span class="php-quote">':'</span>, <span class="php-keyword2">base64_decode</span>(<span class="php-keyword2">substr</span>(<span class="php-var">$this</span>-&gt;input-&gt;server(<span class="php-quote">'HTTP_AUTHORIZATION'</span>), <span class="php-num">6</span>)));
<a href="#928" id="928" class="l"> 928: </a>            }
<a href="#929" id="929" class="l"> 929: </a>        }
<a href="#930" id="930" class="l"> 930: </a>
<a href="#931" id="931" class="l"> 931: </a>        <span class="php-keyword1">if</span> ( ! <span class="php-var">$this</span>-&gt;_check_login(<span class="php-var">$username</span>, <span class="php-var">$password</span>))
<a href="#932" id="932" class="l"> 932: </a>        {
<a href="#933" id="933" class="l"> 933: </a>            <span class="php-var">$this</span>-&gt;_force_login();
<a href="#934" id="934" class="l"> 934: </a>        }
<a href="#935" id="935" class="l"> 935: </a>    }
<a href="#936" id="936" class="l"> 936: </a>
<a href="#937" id="937" class="l"> 937: </a>    <span class="php-comment">/**
</span><a href="#938" id="938" class="l"> 938: </a><span class="php-comment">     * @todo Document this. 
</span><a href="#939" id="939" class="l"> 939: </a><span class="php-comment">     */</span>
<a href="#940" id="940" class="l"> 940: </a>    <span class="php-keyword1">protected</span> <span class="php-keyword1">function</span> _prepare_digest_auth()
<a href="#941" id="941" class="l"> 941: </a>    {
<a href="#942" id="942" class="l"> 942: </a>        <span class="php-comment">// If whitelist is enabled it has the first chance to kick them out</span>
<a href="#943" id="943" class="l"> 943: </a>        <span class="php-keyword1">if</span> (config_item(<span class="php-quote">'rest_ip_whitelist_enabled'</span>))
<a href="#944" id="944" class="l"> 944: </a>        {
<a href="#945" id="945" class="l"> 945: </a>            <span class="php-var">$this</span>-&gt;_check_whitelist_auth();
<a href="#946" id="946" class="l"> 946: </a>        }
<a href="#947" id="947" class="l"> 947: </a>
<a href="#948" id="948" class="l"> 948: </a>        <span class="php-var">$uniqid</span> = <span class="php-keyword2">uniqid</span>(<span class="php-quote">&quot;&quot;</span>); <span class="php-comment">// Empty argument for backward compatibility</span>
<a href="#949" id="949" class="l"> 949: </a>        <span class="php-comment">// We need to test which server authentication variable to use</span>
<a href="#950" id="950" class="l"> 950: </a>        <span class="php-comment">// because the PHP ISAPI module in IIS acts different from CGI</span>
<a href="#951" id="951" class="l"> 951: </a>        <span class="php-keyword1">if</span> (<span class="php-var">$this</span>-&gt;input-&gt;server(<span class="php-quote">'PHP_AUTH_DIGEST'</span>))
<a href="#952" id="952" class="l"> 952: </a>        {
<a href="#953" id="953" class="l"> 953: </a>            <span class="php-var">$digest_string</span> = <span class="php-var">$this</span>-&gt;input-&gt;server(<span class="php-quote">'PHP_AUTH_DIGEST'</span>);
<a href="#954" id="954" class="l"> 954: </a>        }
<a href="#955" id="955" class="l"> 955: </a>        <span class="php-keyword1">elseif</span> (<span class="php-var">$this</span>-&gt;input-&gt;server(<span class="php-quote">'HTTP_AUTHORIZATION'</span>))
<a href="#956" id="956" class="l"> 956: </a>        {
<a href="#957" id="957" class="l"> 957: </a>            <span class="php-var">$digest_string</span> = <span class="php-var">$this</span>-&gt;input-&gt;server(<span class="php-quote">'HTTP_AUTHORIZATION'</span>);
<a href="#958" id="958" class="l"> 958: </a>        }
<a href="#959" id="959" class="l"> 959: </a>        <span class="php-keyword1">else</span>
<a href="#960" id="960" class="l"> 960: </a>        {
<a href="#961" id="961" class="l"> 961: </a>            <span class="php-var">$digest_string</span> = <span class="php-quote">&quot;&quot;</span>;
<a href="#962" id="962" class="l"> 962: </a>        }
<a href="#963" id="963" class="l"> 963: </a>
<a href="#964" id="964" class="l"> 964: </a>        <span class="php-comment">// The $_SESSION['error_prompted'] variable is used to ask the password </span>
<a href="#965" id="965" class="l"> 965: </a>        <span class="php-comment">// again if none given or if the user enters wrong auth information.</span>
<a href="#966" id="966" class="l"> 966: </a>        <span class="php-keyword1">if</span> (<span class="php-keyword1">empty</span>(<span class="php-var">$digest_string</span>))
<a href="#967" id="967" class="l"> 967: </a>        {
<a href="#968" id="968" class="l"> 968: </a>            <span class="php-var">$this</span>-&gt;_force_login(<span class="php-var">$uniqid</span>);
<a href="#969" id="969" class="l"> 969: </a>        }
<a href="#970" id="970" class="l"> 970: </a>
<a href="#971" id="971" class="l"> 971: </a>        <span class="php-comment">// We need to retrieve authentication informations from the $auth_data variable</span>
<a href="#972" id="972" class="l"> 972: </a>        <span class="php-keyword2">preg_match_all</span>(<span class="php-quote">'@(username|nonce|uri|nc|cnonce|qop|response)=[\'&quot;]?([^\'&quot;,]+)@'</span>, <span class="php-var">$digest_string</span>, <span class="php-var">$matches</span>);
<a href="#973" id="973" class="l"> 973: </a>        <span class="php-var">$digest</span> = <span class="php-keyword2">array_combine</span>(<span class="php-var">$matches</span>[<span class="php-num">1</span>], <span class="php-var">$matches</span>[<span class="php-num">2</span>]);
<a href="#974" id="974" class="l"> 974: </a>
<a href="#975" id="975" class="l"> 975: </a>        <span class="php-keyword1">if</span> ( ! <span class="php-keyword2">array_key_exists</span>(<span class="php-quote">'username'</span>, <span class="php-var">$digest</span>) <span class="php-keyword1">OR</span> !<span class="php-var">$this</span>-&gt;_check_login(<span class="php-var">$digest</span>[<span class="php-quote">'username'</span>]))
<a href="#976" id="976" class="l"> 976: </a>        {
<a href="#977" id="977" class="l"> 977: </a>            <span class="php-var">$this</span>-&gt;_force_login(<span class="php-var">$uniqid</span>);
<a href="#978" id="978" class="l"> 978: </a>        }
<a href="#979" id="979" class="l"> 979: </a>
<a href="#980" id="980" class="l"> 980: </a>        <span class="php-var">$valid_logins</span> = &amp; <span class="php-var">$this</span>-&gt;config-&gt;item(<span class="php-quote">'rest_valid_logins'</span>);
<a href="#981" id="981" class="l"> 981: </a>        <span class="php-var">$valid_pass</span> = <span class="php-var">$valid_logins</span>[<span class="php-var">$digest</span>[<span class="php-quote">'username'</span>]];
<a href="#982" id="982" class="l"> 982: </a>
<a href="#983" id="983" class="l"> 983: </a>        <span class="php-comment">// This is the valid response expected</span>
<a href="#984" id="984" class="l"> 984: </a>        <span class="php-var">$A1</span> = <span class="php-keyword2">md5</span>(<span class="php-var">$digest</span>[<span class="php-quote">'username'</span>].<span class="php-quote">':'</span>.<span class="php-var">$this</span>-&gt;config-&gt;item(<span class="php-quote">'rest_realm'</span>).<span class="php-quote">':'</span>.<span class="php-var">$valid_pass</span>);
<a href="#985" id="985" class="l"> 985: </a>        <span class="php-var">$A2</span> = <span class="php-keyword2">md5</span>(<span class="php-keyword2">strtoupper</span>(<span class="php-var">$this</span>-&gt;request-&gt;method).<span class="php-quote">':'</span>.<span class="php-var">$digest</span>[<span class="php-quote">'uri'</span>]);
<a href="#986" id="986" class="l"> 986: </a>        <span class="php-var">$valid_response</span> = <span class="php-keyword2">md5</span>(<span class="php-var">$A1</span>.<span class="php-quote">':'</span>.<span class="php-var">$digest</span>[<span class="php-quote">'nonce'</span>].<span class="php-quote">':'</span>.<span class="php-var">$digest</span>[<span class="php-quote">'nc'</span>].<span class="php-quote">':'</span>.<span class="php-var">$digest</span>[<span class="php-quote">'cnonce'</span>].<span class="php-quote">':'</span>.<span class="php-var">$digest</span>[<span class="php-quote">'qop'</span>].<span class="php-quote">':'</span>.<span class="php-var">$A2</span>);
<a href="#987" id="987" class="l"> 987: </a>
<a href="#988" id="988" class="l"> 988: </a>        <span class="php-keyword1">if</span> (<span class="php-var">$digest</span>[<span class="php-quote">'response'</span>] != <span class="php-var">$valid_response</span>)
<a href="#989" id="989" class="l"> 989: </a>        {
<a href="#990" id="990" class="l"> 990: </a>            <span class="php-keyword2">header</span>(<span class="php-quote">'HTTP/1.0 401 Unauthorized'</span>);
<a href="#991" id="991" class="l"> 991: </a>            <span class="php-keyword2">header</span>(<span class="php-quote">'HTTP/1.1 401 Unauthorized'</span>);
<a href="#992" id="992" class="l"> 992: </a>            <span class="php-keyword1">exit</span>;
<a href="#993" id="993" class="l"> 993: </a>        }
<a href="#994" id="994" class="l"> 994: </a>    }
<a href="#995" id="995" class="l"> 995: </a>
<a href="#996" id="996" class="l"> 996: </a>    <span class="php-comment">/**
</span><a href="#997" id="997" class="l"> 997: </a><span class="php-comment">     * Check if the client's ip is in the 'rest_ip_whitelist' config
</span><a href="#998" id="998" class="l"> 998: </a><span class="php-comment">     */</span>
<a href="#999" id="999" class="l"> 999: </a>    <span class="php-keyword1">protected</span> <span class="php-keyword1">function</span> _check_whitelist_auth()
<a href="#1000" id="1000" class="l">1000: </a>    {
<a href="#1001" id="1001" class="l">1001: </a>        <span class="php-var">$whitelist</span> = <span class="php-keyword2">explode</span>(<span class="php-quote">','</span>, config_item(<span class="php-quote">'rest_ip_whitelist'</span>));
<a href="#1002" id="1002" class="l">1002: </a>
<a href="#1003" id="1003" class="l">1003: </a>        <span class="php-keyword2">array_push</span>(<span class="php-var">$whitelist</span>, <span class="php-quote">'127.0.0.1'</span>, <span class="php-quote">'0.0.0.0'</span>);
<a href="#1004" id="1004" class="l">1004: </a>
<a href="#1005" id="1005" class="l">1005: </a>        <span class="php-keyword1">foreach</span> (<span class="php-var">$whitelist</span> <span class="php-keyword1">AS</span> &amp;<span class="php-var">$ip</span>)
<a href="#1006" id="1006" class="l">1006: </a>        {
<a href="#1007" id="1007" class="l">1007: </a>            <span class="php-var">$ip</span> = <span class="php-keyword2">trim</span>(<span class="php-var">$ip</span>);
<a href="#1008" id="1008" class="l">1008: </a>        }
<a href="#1009" id="1009" class="l">1009: </a>
<a href="#1010" id="1010" class="l">1010: </a>        <span class="php-keyword1">if</span> ( ! <span class="php-keyword2">in_array</span>(<span class="php-var">$this</span>-&gt;input-&gt;ip_address(), <span class="php-var">$whitelist</span>))
<a href="#1011" id="1011" class="l">1011: </a>        {
<a href="#1012" id="1012" class="l">1012: </a>            <span class="php-var">$this</span>-&gt;response(<span class="php-keyword1">array</span>(<span class="php-quote">'status'</span> =&gt; <span class="php-keyword1">false</span>, <span class="php-quote">'error'</span> =&gt; <span class="php-quote">'Not authorized'</span>), <span class="php-num">401</span>);
<a href="#1013" id="1013" class="l">1013: </a>        }
<a href="#1014" id="1014" class="l">1014: </a>    }
<a href="#1015" id="1015" class="l">1015: </a>
<a href="#1016" id="1016" class="l">1016: </a>    <span class="php-comment">/**
</span><a href="#1017" id="1017" class="l">1017: </a><span class="php-comment">     * @todo Document this.
</span><a href="#1018" id="1018" class="l">1018: </a><span class="php-comment">     *
</span><a href="#1019" id="1019" class="l">1019: </a><span class="php-comment">     * @param string $nonce 
</span><a href="#1020" id="1020" class="l">1020: </a><span class="php-comment">     */</span>
<a href="#1021" id="1021" class="l">1021: </a>    <span class="php-keyword1">protected</span> <span class="php-keyword1">function</span> _force_login(<span class="php-var">$nonce</span> = <span class="php-quote">''</span>)
<a href="#1022" id="1022" class="l">1022: </a>    {
<a href="#1023" id="1023" class="l">1023: </a>        <span class="php-keyword1">if</span> (<span class="php-var">$this</span>-&gt;config-&gt;item(<span class="php-quote">'rest_auth'</span>) == <span class="php-quote">'basic'</span>)
<a href="#1024" id="1024" class="l">1024: </a>        {
<a href="#1025" id="1025" class="l">1025: </a>            <span class="php-keyword2">header</span>(<span class="php-quote">'WWW-Authenticate: Basic realm=&quot;'</span>.<span class="php-var">$this</span>-&gt;config-&gt;item(<span class="php-quote">'rest_realm'</span>).<span class="php-quote">'&quot;'</span>);
<a href="#1026" id="1026" class="l">1026: </a>        }
<a href="#1027" id="1027" class="l">1027: </a>        <span class="php-keyword1">elseif</span> (<span class="php-var">$this</span>-&gt;config-&gt;item(<span class="php-quote">'rest_auth'</span>) == <span class="php-quote">'digest'</span>)
<a href="#1028" id="1028" class="l">1028: </a>        {
<a href="#1029" id="1029" class="l">1029: </a>            <span class="php-keyword2">header</span>(<span class="php-quote">'WWW-Authenticate: Digest realm=&quot;'</span>.<span class="php-var">$this</span>-&gt;config-&gt;item(<span class="php-quote">'rest_realm'</span>).<span class="php-quote">'&quot;, qop=&quot;auth&quot;, nonce=&quot;'</span>.<span class="php-var">$nonce</span>.<span class="php-quote">'&quot;, opaque=&quot;'</span>.<span class="php-keyword2">md5</span>(<span class="php-var">$this</span>-&gt;config-&gt;item(<span class="php-quote">'rest_realm'</span>)).<span class="php-quote">'&quot;'</span>);
<a href="#1030" id="1030" class="l">1030: </a>        }
<a href="#1031" id="1031" class="l">1031: </a>
<a href="#1032" id="1032" class="l">1032: </a>        <span class="php-var">$this</span>-&gt;response(<span class="php-keyword1">array</span>(<span class="php-quote">'status'</span> =&gt; <span class="php-keyword1">false</span>, <span class="php-quote">'error'</span> =&gt; <span class="php-quote">'Not authorized'</span>), <span class="php-num">401</span>);
<a href="#1033" id="1033" class="l">1033: </a>    }
<a href="#1034" id="1034" class="l">1034: </a>
<a href="#1035" id="1035" class="l">1035: </a>    <span class="php-comment">/**
</span><a href="#1036" id="1036" class="l">1036: </a><span class="php-comment">     * Force it into an array
</span><a href="#1037" id="1037" class="l">1037: </a><span class="php-comment">     *
</span><a href="#1038" id="1038" class="l">1038: </a><span class="php-comment">     * @param object|array $data
</span><a href="#1039" id="1039" class="l">1039: </a><span class="php-comment">     * @return array 
</span><a href="#1040" id="1040" class="l">1040: </a><span class="php-comment">     */</span>
<a href="#1041" id="1041" class="l">1041: </a>    <span class="php-keyword1">protected</span> <span class="php-keyword1">function</span> _force_loopable(<span class="php-var">$data</span>)
<a href="#1042" id="1042" class="l">1042: </a>    {
<a href="#1043" id="1043" class="l">1043: </a>        <span class="php-comment">// Force it to be something useful</span>
<a href="#1044" id="1044" class="l">1044: </a>        <span class="php-keyword1">if</span> ( ! <span class="php-keyword2">is_array</span>(<span class="php-var">$data</span>) <span class="php-keyword1">AND</span> !<span class="php-keyword2">is_object</span>(<span class="php-var">$data</span>))
<a href="#1045" id="1045" class="l">1045: </a>        {
<a href="#1046" id="1046" class="l">1046: </a>            <span class="php-var">$data</span> = (<span class="php-keyword1">array</span>) <span class="php-var">$data</span>;
<a href="#1047" id="1047" class="l">1047: </a>        }
<a href="#1048" id="1048" class="l">1048: </a>
<a href="#1049" id="1049" class="l">1049: </a>        <span class="php-keyword1">return</span> <span class="php-var">$data</span>;
<a href="#1050" id="1050" class="l">1050: </a>    }
<a href="#1051" id="1051" class="l">1051: </a>
<a href="#1052" id="1052" class="l">1052: </a>    <span class="php-comment">// FORMATING FUNCTIONS ---------------------------------------------------------</span>
<a href="#1053" id="1053" class="l">1053: </a>    <span class="php-comment">// Many of these have been moved to the Format class for better separation, but these methods will be checked too</span>
<a href="#1054" id="1054" class="l">1054: </a>
<a href="#1055" id="1055" class="l">1055: </a>    <span class="php-comment">/**
</span><a href="#1056" id="1056" class="l">1056: </a><span class="php-comment">     * Encode as JSONP
</span><a href="#1057" id="1057" class="l">1057: </a><span class="php-comment">     * 
</span><a href="#1058" id="1058" class="l">1058: </a><span class="php-comment">     * @param array $data The input data.
</span><a href="#1059" id="1059" class="l">1059: </a><span class="php-comment">     * @return string The JSONP data string (loadable from Javascript). 
</span><a href="#1060" id="1060" class="l">1060: </a><span class="php-comment">     */</span>
<a href="#1061" id="1061" class="l">1061: </a>    <span class="php-keyword1">protected</span> <span class="php-keyword1">function</span> _format_jsonp(<span class="php-var">$data</span> = <span class="php-keyword1">array</span>())
<a href="#1062" id="1062" class="l">1062: </a>    {
<a href="#1063" id="1063" class="l">1063: </a>        <span class="php-keyword1">return</span> <span class="php-var">$this</span>-&gt;get(<span class="php-quote">'callback'</span>).<span class="php-quote">'('</span>.<span class="php-keyword2">json_encode</span>(<span class="php-var">$data</span>).<span class="php-quote">')'</span>;
<a href="#1064" id="1064" class="l">1064: </a>    }
<a href="#1065" id="1065" class="l">1065: </a>
<a href="#1066" id="1066" class="l">1066: </a>}</code></pre>

	<div id="footer">
		 API documentation generated by <a href="http://apigen.org">ApiGen 2.6.1</a>
	</div>
</div>
</div>
</body>
</html>
